1 | // ----------------------------------------------------------------------- |
---|
2 | // get_random_spectrum.cc: Functions to obtain random values. |
---|
3 | // ----------------------------------------------------------------------- |
---|
4 | // Copyright (C) 2006, Matthew Whiting, ATNF |
---|
5 | // |
---|
6 | // This program is free software; you can redistribute it and/or modify it |
---|
7 | // under the terms of the GNU General Public License as published by the |
---|
8 | // Free Software Foundation; either version 2 of the License, or (at your |
---|
9 | // option) any later version. |
---|
10 | // |
---|
11 | // Duchamp is distributed in the hope that it will be useful, but WITHOUT |
---|
12 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
13 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
14 | // for more details. |
---|
15 | // |
---|
16 | // You should have received a copy of the GNU General Public License |
---|
17 | // along with Duchamp; if not, write to the Free Software Foundation, |
---|
18 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
---|
19 | // |
---|
20 | // Correspondence concerning Duchamp may be directed to: |
---|
21 | // Internet email: Matthew.Whiting [at] atnf.csiro.au |
---|
22 | // Postal address: Dr. Matthew Whiting |
---|
23 | // Australia Telescope National Facility, CSIRO |
---|
24 | // PO Box 76 |
---|
25 | // Epping NSW 1710 |
---|
26 | // AUSTRALIA |
---|
27 | // ----------------------------------------------------------------------- |
---|
28 | #include <iostream> |
---|
29 | #include <math.h> |
---|
30 | |
---|
31 | float getNormalRV() |
---|
32 | { |
---|
33 | float v1,v2,s; |
---|
34 | // simulate a standard normal RV via polar method |
---|
35 | do{ |
---|
36 | v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.; |
---|
37 | v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.; |
---|
38 | s = v1*v1+v2*v2; |
---|
39 | }while(s>1); |
---|
40 | return sqrt(-2.*log(s)/s)*v1; |
---|
41 | |
---|
42 | } |
---|
43 | |
---|
44 | float getNormalRVtrunc() |
---|
45 | { |
---|
46 | float v1,v2,s; |
---|
47 | // simulate a standard normal RV via polar method |
---|
48 | do{ |
---|
49 | v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.; |
---|
50 | v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.; |
---|
51 | s = v1*v1+v2*v2; |
---|
52 | }while((s>1)||(fabs(sqrt(-2.*log(s)/s)*v1)>sqrt(2*M_LN2))); |
---|
53 | return sqrt(-2.*log(s)/s)*v1; |
---|
54 | |
---|
55 | } |
---|
56 | |
---|
57 | float getNormalRV(float mean, float sigma) |
---|
58 | { |
---|
59 | // simulate a standard normal RV via polar method |
---|
60 | float v1,v2,s; |
---|
61 | do{ |
---|
62 | v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.; |
---|
63 | v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.; |
---|
64 | s = v1*v1+v2*v2; |
---|
65 | }while(s>1); |
---|
66 | float z=sqrt(-2.*log(s)/s)*v1; |
---|
67 | return z*sigma + mean; |
---|
68 | } |
---|
69 | |
---|
70 | void getRandomSpectrum(int length, float *x, float *y) |
---|
71 | { |
---|
72 | srandom(time(0)); |
---|
73 | rand(); |
---|
74 | for(int i=0;i<length;i++){ |
---|
75 | x[i] = (float)i; |
---|
76 | // simulate a standard normal RV via polar method |
---|
77 | double v1,v2,s; |
---|
78 | do{ |
---|
79 | v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.; |
---|
80 | v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.; |
---|
81 | s = v1*v1+v2*v2; |
---|
82 | }while(s>1); |
---|
83 | y[i] = sqrt(-2.*log(s)/s)*v1; |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | void getRandomSpectrum(int length, float *x, double *y) |
---|
88 | { |
---|
89 | // srandomdev(); |
---|
90 | srandom(time(0)); |
---|
91 | rand(); |
---|
92 | for(int i=0;i<length;i++){ |
---|
93 | x[i] = (float)i; |
---|
94 | // simulate a standard normal RV via polar method |
---|
95 | double v1,v2,s; |
---|
96 | do{ |
---|
97 | v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.; |
---|
98 | v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.; |
---|
99 | s = v1*v1+v2*v2; |
---|
100 | }while(s>1); |
---|
101 | y[i] = sqrt(-2.*log(s)/s)*v1; |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | void getRandomSpectrum(int length, float mean, float sigma, |
---|
107 | float *x, double *y) |
---|
108 | { |
---|
109 | // srandomdev(); |
---|
110 | srandom(time(0)); |
---|
111 | rand(); |
---|
112 | for(int i=0;i<length;i++){ |
---|
113 | x[i] = (float)i; |
---|
114 | // simulate a standard normal RV via polar method |
---|
115 | double v1,v2,s; |
---|
116 | do{ |
---|
117 | v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.; |
---|
118 | v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.; |
---|
119 | s = v1*v1+v2*v2; |
---|
120 | }while(s>1); |
---|
121 | float z = sqrt(-2.*log(s)/s)*v1; |
---|
122 | y[i] = z * sigma + mean; |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|