-
Notifications
You must be signed in to change notification settings - Fork 0
/
rwwb_common.h
79 lines (57 loc) · 1.35 KB
/
rwwb_common.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
70
71
72
73
74
75
76
77
78
79
#ifndef RWWB_COMMON_H_INCLUDED
#define RWWB_COMMON_H_INCLUDED
#include <stdio.h>
#include <math.h>
#ifndef BOOL
typedef unsigned int BOOL;
#endif
#ifndef TRUE
#define TRUE (-1)
#endif
#ifndef FALSE
#define FALSE (0)
#endif
#ifndef __min
#define __min(arg1, arg2) ((arg1)<(arg2)?arg1:arg2)
#endif
// returns a random number between 0.0 and fMaxValue
double randomNumber(double fMaxValue);
// returns a random angle between 0.0 and 2*pi
double randomAngle();
// set the random number seed, if pnSeed != NULL set *pnSeed to the seed used
// return TRUE at success, otherwise return FALSE
BOOL randomSeed(unsigned int *pnSeed = NULL);
// returns the square of fValue
double square(double fValue);
// definitions
inline double randomNumber(double fMaxValue)
{
return fMaxValue*((double)rand()/((double)RAND_MAX));
}
inline double randomAngle()
{
return randomNumber(2.0*M_PI);
}
inline BOOL randomSeed(unsigned int *pnSeed)
{
FILE *pRandomFile;
unsigned int nSeed;
pRandomFile = fopen("/dev/urandom", "rb");
if(pRandomFile)
{
fread(&nSeed, sizeof(nSeed), 1, pRandomFile);
fclose(pRandomFile);
if(pnSeed)
{
*pnSeed = nSeed;
}
srand(nSeed);
return TRUE;
}
return FALSE;
}
inline double square(double fValue)
{
return fValue * fValue;
}
#endif // RWWB_COMMON_H_INCLUDED