-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandgen.h
executable file
·69 lines (61 loc) · 3 KB
/
randgen.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifndef _RANDGEN_H
#define _RANDGEN_H
// RandGen objects provide a source of computer-generated random numbers
// (sometimes known as pseudo-random numbers).
//
// By default, a RandGen object will produce a different series of
// numbers (through repeated calls to the RandInt and RandDouble
// methods) every time the program is run. When testing, though, it is
// often useful to have a program generate the same sequence of numbers
// each time it is run; this can be achieved by specifying a "seed" when
// the first RandGen object in the program is created.
//
// To construct random integers in a given range, client programs
// should use RandInt. To construct random doubles, client programs
// should use RandReal. The ranges for the return values for these
// functions are indicated with mathematical notation:
// [0..max) means a number between 0 and max,
// not including max but including 0;
// [0..max] means a number between 0 and max, including
// both 0 and max.
//
// For example,
// RandGen r;
// r.RandInt(5) an int between 0 and 5, not including 5
// r.RandInt(0, 5) an int between 0 and 5, might include 0 or 5
// r.RandReal() a double between 0 and 1, not including 1
// r.RandReal(4, 6) a double between 4 and 6, might include 4 or 6
//
// Technical Note:
// The "seed" used by all random number generation in a program is set
// the first time a random number generator object is constructed by
// the program. All other random number generator objects created
// later in the program will use the same seed.
class RandGen
{
public:
// Constructors
// If the first RandGen object is constructed with the default
// constructor, a different series of numbers is produced every
// time the program is run. If the first RandGen object is
// constructed with a seed, the same series of numbers is produced
// every time.
RandGen(); // default constructor
RandGen(int seed); // produce same series every time
// (most useful during testing)
// Accessing functions
// Ranges for return values are indicated with mathematical
// notation:
// [0..max) means a number between 0 and max,
// not including max but including 0;
// [0..max] means a number between 0 and max, including
// both 0 and max.
int RandInt(int max); // returns int in [0..max)
int RandInt(int low, int max); // returns int in [low..max]
double RandReal(); // returns double in [0..1)
double RandReal(double low, double max); // returns double in
// [low..max]
private:
static int ourInitialized; // for 'per-class' initialization
};
#endif // _RANDGEN_H not defined