-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandom.cpp
More file actions
48 lines (39 loc) · 929 Bytes
/
Random.cpp
File metadata and controls
48 lines (39 loc) · 929 Bytes
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
#include "Random.h"
#include <time.h>
#include <stdlib.h>
Random::Random()
{
randomInit();
}
Random::~Random()
{
}
Random* Random::getRandom()
{
static Random random; //initialized when method is called the first time
return &random;
}
void Random::randomInit()
{
srand (time(NULL));
//try to get a more random starting number
rand();
rand();
rand();
rand();
}
int Random::getRandomInt(int lower, int upper)
{
int diff = upper - lower + 1;
int random_num = rand()%diff;
random_num = random_num + lower; //gives a number between lower and upper, inclusive
return random_num;
}
float Random::getRandomFloat(float lower, float upper)
{
float r_float_1 = (float) rand();
float r_float_2 = (float) RAND_MAX;
float random_normalized = r_float_1/r_float_2; //between 0.0 and 1.0
float random_float = lower + random_normalized*(upper - lower);
return random_float;
}