ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/mdtools/md_code/randomSPRNG.cpp
Revision: 253
Committed: Thu Jan 30 15:20:21 2003 UTC (21 years, 5 months ago) by chuckv
File size: 1194 byte(s)
Log Message:
Added a generic util code directory and moved Linux_ifc_machdep to it.
MPI changes to compile MPI modules.

File Contents

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