-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandgen.cpp
executable file
·70 lines (60 loc) · 1.73 KB
/
randgen.cpp
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
70
#include <time.h> // for time()
#include <stdlib.h> // for rand/srand
#include "randgen.h"
//#include <cmath> // *** necessary?
int RandGen::ourInitialized = 0;
// constructors
RandGen::RandGen()
// postcondition: system srand() used to initialize seed
// once per program
{
if (0 == ourInitialized)
{
ourInitialized = 1; // only call srand once
srand(unsigned(time(0))); // randomize
}
}
RandGen::RandGen(int seed)
// postcondition: system srand() used to initialize seed
// once per program
{
if (0 == ourInitialized)
{
ourInitialized = 1; // only call srand once
srand(seed); // randomize
}
}
// public accessing functions
int RandGen::RandInt(int max)
// precondition: max > 0
// postcondition: returns int in [0..max)
{
return int(RandReal() * max);
}
int RandGen::RandInt(int min, int max)
// precondition: min <= max
// postcondition: returns int in [min..max]
{
return min + RandInt(max - min + 1);
}
double RandGen::RandReal()
// postcondition: returns double in [0..1)
{
// RAND_MAX is defined in <stdlib.h>
return rand() / (double(RAND_MAX) + 1);
}
/* *** Old version: why inconsistent with RandInt?
double RandGen::RandReal(double low, double high)
{
double width = fabs(high-low);
double thelow = low < high ? low : high;
return (RandReal() * width) + thelow;
}
*/
double RandGen::RandReal(double dmin, double dmax)
// precondition: dmin <= dmax
// postcondition: returns double in [dmin..dmax)
{
double width = dmax - dmin;
return RandReal() * width + dmin;
}