Galactic Bloodshed
rand.cc
Go to the documentation of this file.
1 // Copyright 2014 The Galactic Bloodshed Authors. All rights reserved.
2 // Use of this source code is governed by a license that can be
3 // found in the COPYING file.
4 
5 /* Random number generator */
6 
7 #include "gb/utils/rand.h"
8 
9 #include <cstdlib>
10 
11 /* double double_rand() this returns a random number between 0 and 1 */
12 double double_rand() { return (double)random() / 2147483648.0; }
13 
14 /* int int_rand(low,hi) - this returns an integer random number
15  * between hi and low, inclusive. */
16 long long_rand(long low, long hi) {
17  return ((hi <= low) ? low : (random() % (hi - low + 1)) + low);
18 }
19 
20 /* int int_rand(low,hi) - this returns an integer random number
21  * between hi and low, inclusive. */
22 int int_rand(int low, int hi) {
23  return ((hi <= low) ? low : (random() % (hi - low + 1)) + low);
24 }
25 
26 /* int round_rand(double) - returns double rounded to integer, with
27  * proportional chance of rounding up or
28  * down. */
29 int round_rand(double x) {
30  return ((double_rand() > (x - (double)((int)x))) ? (int)x : (int)(x + 1));
31 }
32 
33 bool success(int x) { return int_rand(1, 100) <= (x); }
long long_rand(long low, long hi)
Definition: rand.cc:16
int round_rand(double x)
Definition: rand.cc:29
int int_rand(int low, int hi)
Definition: rand.cc:22
bool success(int x)
Definition: rand.cc:33
double double_rand()
Definition: rand.cc:12