-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgauss.cc
executable file
·77 lines (58 loc) · 1.44 KB
/
gauss.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// a simple Gaussian random number generator using accept/reject method
// samples within 3 sigma of mean
using namespace std;//AWS20050624
#include<iostream> //AWS20050624
#include<cmath> //AWS20050624
#include<cstdlib> // IMOS20010816 AWS20050624
static int init=0;
double gauss(double mean, double sigma){
double x,y;
double xmin,xmax;
double v;
unsigned seed;
if(sigma==0.0){x=mean;return x;} // otherwise loops forever for this case
xmin=mean-sigma*3.0;
xmax=mean+sigma*3.0;
if(init==0){
init =1;
seed=1234;
srand(seed);
}
int accept=0;
while(accept==0){
x=xmin+double(rand())/RAND_MAX*(xmax-xmin);
y=double(rand())/RAND_MAX;
v=exp(-(x-mean)*(x-mean)/(2.0*sigma*sigma));
if(y < v) accept=1;
//cout<<x<<" "<<y<<" "<<v<<" "<<accept<<endl;
}
return x;
}
/////////////////////////////////////////////
int test_gauss(){
double mean, sigma;
mean=20.0;
sigma=5.0;
double g;
int nrept=20;
int n=1000;
cout<<"test_gauss:"<<endl;
cout<<"number of samples="<<n<<endl;
cout<<"number of runs ="<<nrept<<endl;
cout<<" true mean="<< mean<<" true sigma="<< sigma<<endl;
for (int irept=0;irept<nrept;irept++){
float sum=0.;
float sumsq=0.;
for (int i=0;i<n;i++){
g=gauss(mean,sigma);
sum+=g;
sumsq+=g*g;
//cout<<g<<endl;
}
float gmean=sum/n;
float gsigma=sqrt((sumsq-2*gmean*sum + gmean*gmean*n)/n);
cout<<"sample mean="<<gmean<< " sample sigma="<<gsigma<<endl;
}
cout<<endl;
return 0;}
//int main(){test_gauss();return 0;} // use for standalone test