source: trunk/src/Utils/get_random_spectrum.cc @ 125

Last change on this file since 125 was 125, checked in by Matthew Whiting, 18 years ago

Changed all instances of fabsf to fabs so that compilation on venice works.

File size: 2.5 KB
Line 
1#include <iostream>
2#include <stdlib.h>
3#include <time.h>
4#include <math.h>
5
6float getNormalRV()
7{
8  float v1,v2,s;
9  do{
10    v1 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
11    v2 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
12    s = v1*v1+v2*v2;
13  }while(s>1);
14  return sqrt(-2.*log(s)/s)*v1;
15
16}
17
18float getNormalRVtrunc()
19{
20  float v1,v2,s;
21  do{
22    v1 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
23    v2 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
24    s = v1*v1+v2*v2;
25  }while((s>1)||(fabs(sqrt(-2.*log(s)/s)*v1)>sqrt(2*M_LN2)));
26  return sqrt(-2.*log(s)/s)*v1;
27
28}
29
30float getNormalRV(float mean, float sigma)
31{
32  float v1,v2,s;
33  do{
34    v1 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
35    v2 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
36    s = v1*v1+v2*v2;
37  }while(s>1);
38  float z=sqrt(-2.*log(s)/s)*v1;
39  return z*sigma + mean;
40}
41
42void getRandomSpectrum(int length, float *x, float *y)
43{
44  srandom(time(0));
45  rand();
46
47  for(int i=0;i<length;i++){
48    x[i] = i;
49    // simulate a standard normal RV via polar method
50    float v1,v2,s;
51    do{
52      v1 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
53      v2 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
54      s = v1*v1+v2*v2;
55    }while(s>1);
56    y[i] = sqrt(-2.*log(s)/s)*v1;
57  }
58}
59
60void getRandomSpectrum(int length, float *x, double *y)
61{
62  srandom(time(0));
63  rand();
64  for(int i=0;i<length;i++){
65    x[i] = (float)i;
66    // simulate a standard normal RV via polar method
67    double v1,v2,s;
68    do{
69      v1 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
70      v2 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
71      s = v1*v1+v2*v2;
72    }while(s>1);
73    y[i] = sqrt(-2.*log(s)/s)*v1;
74//     cerr<< x[i]<<" " << y[i]<<endl;
75  }
76}
77
78
79void getRandomSpectrum(int length, float mean, float sigma, float *x, double *y)
80{
81  srandom(time(0));
82  rand();
83  for(int i=0;i<length;i++){
84    x[i] = (float)i;
85    // simulate a standard normal RV via polar method
86    double v1,v2,s;
87    do{
88      v1 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
89      v2 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
90      s = v1*v1+v2*v2;
91    }while(s>1);
92    float z = sqrt(-2.*log(s)/s)*v1;
93    y[i] = z * sigma + mean;
94//     cerr<< x[i]<<" " << y[i]<<endl;
95  }
96}
97
98void getRandomSpectrum(int length, float mean, float sigma, float *x, float *y)
99{
100  srandom(time(0));
101  rand();
102  for(int i=0;i<length;i++){
103    x[i] = (float)i;
104    // simulate a standard normal RV via polar method
105    float v1,v2,s;
106    do{
107      v1 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
108      v2 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
109      s = v1*v1+v2*v2;
110    }while(s>1);
111    float z = sqrt(-2.*log(s)/s)*v1;
112    y[i] = z * sigma + mean;
113//     cerr<< x[i]<<" " << y[i]<<endl;
114  }
115}
116
Note: See TracBrowser for help on using the repository browser.