source: tags/release-1.1.7/src/Devel/get_random_spectrum.cc @ 1455

Last change on this file since 1455 was 518, checked in by MatthewWhiting, 15 years ago

Using a better random number generator.

File size: 3.5 KB
Line 
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
31float 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
44float 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
57float 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
70void 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
87void getRandomSpectrum(int length, float *x, double *y)
88{
89  srandomdev();
90  rand();
91  for(int i=0;i<length;i++){
92    x[i] = (float)i;
93    // simulate a standard normal RV via polar method
94    double v1,v2,s;
95    do{
96      v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
97      v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
98      s = v1*v1+v2*v2;
99    }while(s>1);
100    y[i] = sqrt(-2.*log(s)/s)*v1;
101  }
102}
103
104
105void getRandomSpectrum(int length, float mean, float sigma,
106                       float *x, double *y)
107{
108  srandomdev();
109  rand();
110  for(int i=0;i<length;i++){
111    x[i] = (float)i;
112    // simulate a standard normal RV via polar method
113    double v1,v2,s;
114    do{
115      v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
116      v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
117      s = v1*v1+v2*v2;
118    }while(s>1);
119    float z = sqrt(-2.*log(s)/s)*v1;
120    y[i] = z * sigma + mean;
121  }
122}
123
Note: See TracBrowser for help on using the repository browser.