1 |
#include <iostream> |
2 |
#include <math.h> |
3 |
|
4 |
#include "math/randomSPRNG.hpp" |
5 |
#include "utils/simError.h" |
6 |
#include <sprng.h> |
7 |
|
8 |
#ifdef IS_MPI |
9 |
|
10 |
#include <mpi.h> |
11 |
#endif |
12 |
|
13 |
using namespace std; |
14 |
|
15 |
/* randomStreamSPRNF creates a new SPRNG stream for random numbers |
16 |
*/ |
17 |
|
18 |
int randomSPRNG::nStreamsInitialized = 0; |
19 |
|
20 |
randomSPRNG::randomSPRNG( int iseed ) { |
21 |
int newSeed; |
22 |
nStreamsInitialized++; |
23 |
newSeed = abs(iseed) + nStreamsInitialized; |
24 |
|
25 |
if (newSeed < 0) |
26 |
newSeed = abs(newSeed); |
27 |
|
28 |
#ifdef IS_MPI |
29 |
|
30 |
//random number generator should not depend on MpiSimulation |
31 |
MPI_Comm_size(MPI_COMM_WORLD, &nSPRNGStreams); |
32 |
myStreamNumber = worldRank; |
33 |
|
34 |
#else |
35 |
|
36 |
nSPRNGStreams = 1; |
37 |
myStreamNumber = 0; |
38 |
|
39 |
#endif |
40 |
|
41 |
thisStream = init_sprng(GTYPE, myStreamNumber, nSPRNGStreams, newSeed, |
42 |
SPRNG_DEFAULT); |
43 |
} |
44 |
|
45 |
randomSPRNG::~randomSPRNG() { |
46 |
if (thisStream != NULL) { |
47 |
free_sprng(thisStream); |
48 |
nStreamsInitialized--; |
49 |
} |
50 |
} |
51 |
|
52 |
double randomSPRNG::getRandom() { return sprng(thisStream); } |
53 |
|
54 |
// Gaussian SPRNG class... |
55 |
|
56 |
double gaussianSPRNG::getGaussian() { |
57 |
double ranNum1; |
58 |
double ranNum2; |
59 |
double gaussianNumber; |
60 |
|
61 |
ranNum1 = getRandom(); |
62 |
ranNum2 = getRandom(); |
63 |
|
64 |
gaussianNumber = sqrt(-2.0 * log(ranNum1)) * cos(2 * M_PI * ranNum2); |
65 |
|
66 |
return gaussianNumber; |
67 |
} |