Compressed Sparse Blocks  1.2
randgen.h
Go to the documentation of this file.
1 #ifndef _RANDGEN_H
2 #define _RANDGEN_H
3
4
5 #include <ctime> // for time()
6 #include <cstdlib>
7 #include <iostream>
8 #include <cmath>
9 #include <climits> // for INT_MAX
10
11 // designed for implementation-independent randomization
12 // if all system-dependent calls included in this class, then
13 // other classes can make use of this class in independent manner
14 // all random numbers are uniformly distributed in given range
15 //
16 // RandGen() --- constructor sets seed of random # generator
17 // once per program, not per class/object
18 //
19 // RandInt(int max)
20 // RandInt(int low,int max) - return random integer in range [0..max)
21 // when one parameter used, [low..max] when
22 // two parameters used
23 //
24 // examples: rnd.RandInt(6) is random integer [0..5] or [0..6)
25 // rnd.RandInt(3,10) is random integer [3..10]
26 // rnd.RandInt() is random integer [0..INT_MAX)
27 //
28 // RandReal() -- returns random double in range [0..1)
29 // RandReal(double low, double max) -- random double in range [low..max)
30
31 class RandGen
32 {
33  public:
34  RandGen(); // set seed for all instances
35  int RandInt(int max = INT_MAX); // returns int in [0..max)
36  int RandInt(int low, int max); // returns int in [low..max]
37  double RandReal(); // returns double in [0..1)
38  double RandReal(double low, double max); // range [low..max]
39
40  static void SetSeed(int seed); // static (per class) seed set
41 private:
42  static int ourInitialized; // for 'per-class' initialization
43 };
44
45
46 /*************** DEFINITIONS ****************/
47
48 int RandGen::ourInitialized = 0;
49
50 void RandGen::SetSeed(int seed)
51 // postcondition: system srand() used to initialize seed
52 // once per program (this is a static function)
53 {
54  if (0 == ourInitialized)
55  { ourInitialized = 1; // only call srand once
56  srand(seed); // randomize
57  }
58 }
59
60
62 // postcondition: system srand() used to initialize seed
63 // once per program
64 {
65  if (0 == ourInitialized)
66  { ourInitialized = 1; // only call srand once
67  srand(unsigned(time(0))); // randomize
68  }
69 }
70
71 int RandGen::RandInt(int max)
72 // precondition: max > 0
73 // postcondition: returns int in [0..max)
74 {
75  return int(RandReal() * max);
76 }
77
78 int RandGen::RandInt(int low, int max)
79 // precondition: low <= max
80 // postcondition: returns int in [low..max]
81 {
82  return low + RandInt(max-low+1);
83 }
84
86 // postcondition: returns double in [0..1)
87 {
88  return rand() / (double(RAND_MAX) + 1);
89 }
90
91 double RandGen::RandReal(double low, double high)
92 {
93  double width = fabs(high-low);
94  double thelow = low < high ? low : high;
95  return RandReal()*width + thelow;
96 }
97
98 #endif
RandGen()
Definition: randgen.h:61
double RandReal()
Definition: randgen.h:85
static void SetSeed(int seed)
Definition: randgen.h:50
int RandInt(int max=INT_MAX)
Definition: randgen.h:71