source: trunk/src/Devel/get_random_spectrum.cc @ 722

Last change on this file since 722 was 722, checked in by MatthewWhiting, 14 years ago

Commenting out srandomdev as this doesn't seem to work on linux

File size: 3.5 KB
RevLine 
[301]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// -----------------------------------------------------------------------
[3]28#include <iostream>
29#include <math.h>
30
31float getNormalRV()
32{
33  float v1,v2,s;
[146]34  // simulate a standard normal RV via polar method
[3]35  do{
[518]36    v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
37    v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
[3]38    s = v1*v1+v2*v2;
39  }while(s>1);
40  return sqrt(-2.*log(s)/s)*v1;
41
42}
43
[70]44float getNormalRVtrunc()
45{
46  float v1,v2,s;
[146]47  // simulate a standard normal RV via polar method
[70]48  do{
[518]49    v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
50    v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
[70]51    s = v1*v1+v2*v2;
[125]52  }while((s>1)||(fabs(sqrt(-2.*log(s)/s)*v1)>sqrt(2*M_LN2)));
[70]53  return sqrt(-2.*log(s)/s)*v1;
54
55}
56
[3]57float getNormalRV(float mean, float sigma)
58{
[146]59  // simulate a standard normal RV via polar method
[3]60  float v1,v2,s;
61  do{
[518]62    v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
63    v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
[3]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
[201]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{
[518]79      v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
80      v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
[201]81      s = v1*v1+v2*v2;
82    }while(s>1);
83    y[i] = sqrt(-2.*log(s)/s)*v1;
84  }
85}
86
[3]87void getRandomSpectrum(int length, float *x, double *y)
88{
[722]89//   srandomdev();
90  srandom(time(0));
[3]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{
[518]97      v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
98      v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
[3]99      s = v1*v1+v2*v2;
100    }while(s>1);
101    y[i] = sqrt(-2.*log(s)/s)*v1;
102  }
103}
104
105
[146]106void getRandomSpectrum(int length, float mean, float sigma,
107                       float *x, double *y)
[3]108{
[722]109//   srandomdev();
110  srandom(time(0));
[3]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{
[518]117      v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
118      v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
[3]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
Note: See TracBrowser for help on using the repository browser.