ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/oopse-1.0/libmdtools/randomSPRNG.cpp
Revision: 1447
Committed: Fri Jul 30 21:01:35 2004 UTC (19 years, 11 months ago) by gezelter
File size: 1190 byte(s)
Log Message:
Initial import of OOPSE sources into cvs tree

File Contents

# User Rev Content
1 gezelter 1447 #include <iostream>
2     #include <math.h>
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->getNProcessors();
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     }