ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-2.0/src/math/randomSPRNG.cpp
Revision: 1683
Committed: Thu Oct 28 22:34:02 2004 UTC (19 years, 8 months ago)
File size: 1208 byte(s)
Log Message:
This commit was manufactured by cvs2svn to create branch 'new_design'.

File Contents

# User Rev Content
1 gezelter 1490 #include <iostream>
2     #include <math.h>
3    
4 tim 1492 #include "math/randomSPRNG.hpp"
5     #include "utils/simError.h"
6 gezelter 1490 #include <sprng.h>
7    
8     #ifdef IS_MPI
9 tim 1492 #include "brains/mpiSimulation.hpp"
10 gezelter 1490 #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     }