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

Last change on this file since 1018 was 876, checked in by MatthewWhiting, 12 years ago

Getting include statements right for the Devel part

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>
[876]30#include <stdlib.h>
[3]31
32float getNormalRV()
33{
34  float v1,v2,s;
[146]35  // simulate a standard normal RV via polar method
[3]36  do{
[518]37    v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
38    v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
[3]39    s = v1*v1+v2*v2;
40  }while(s>1);
41  return sqrt(-2.*log(s)/s)*v1;
42
43}
44
[70]45float getNormalRVtrunc()
46{
47  float v1,v2,s;
[146]48  // simulate a standard normal RV via polar method
[70]49  do{
[518]50    v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
51    v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
[70]52    s = v1*v1+v2*v2;
[125]53  }while((s>1)||(fabs(sqrt(-2.*log(s)/s)*v1)>sqrt(2*M_LN2)));
[70]54  return sqrt(-2.*log(s)/s)*v1;
55
56}
57
[3]58float getNormalRV(float mean, float sigma)
59{
[146]60  // simulate a standard normal RV via polar method
[3]61  float v1,v2,s;
62  do{
[518]63    v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
64    v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
[3]65    s = v1*v1+v2*v2;
66  }while(s>1);
67  float z=sqrt(-2.*log(s)/s)*v1;
68  return z*sigma + mean;
69}
70
[201]71void getRandomSpectrum(int length, float *x, float *y)
72{
[860]73  // srandom(time(0));
[201]74  rand();
75  for(int i=0;i<length;i++){
76    x[i] = (float)i;
77    // simulate a standard normal RV via polar method
78    double v1,v2,s;
79    do{
[518]80      v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
81      v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
[201]82      s = v1*v1+v2*v2;
83    }while(s>1);
84    y[i] = sqrt(-2.*log(s)/s)*v1;
85  }
86}
87
[3]88void getRandomSpectrum(int length, float *x, double *y)
89{
[722]90//   srandomdev();
91  srandom(time(0));
[3]92  rand();
93  for(int i=0;i<length;i++){
94    x[i] = (float)i;
95    // simulate a standard normal RV via polar method
96    double v1,v2,s;
97    do{
[518]98      v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
99      v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
[3]100      s = v1*v1+v2*v2;
101    }while(s>1);
102    y[i] = sqrt(-2.*log(s)/s)*v1;
103  }
104}
105
106
[146]107void getRandomSpectrum(int length, float mean, float sigma,
108                       float *x, double *y)
[3]109{
[722]110//   srandomdev();
111  srandom(time(0));
[3]112  rand();
113  for(int i=0;i<length;i++){
114    x[i] = (float)i;
115    // simulate a standard normal RV via polar method
116    double v1,v2,s;
117    do{
[518]118      v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
119      v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
[3]120      s = v1*v1+v2*v2;
121    }while(s>1);
122    float z = sqrt(-2.*log(s)/s)*v1;
123    y[i] = z * sigma + mean;
124  }
125}
126
Note: See TracBrowser for help on using the repository browser.