source: tags/release-1.1.4/src/Devel/get_random_spectrum.cc @ 1441

Last change on this file since 1441 was 301, checked in by Matthew Whiting, 17 years ago

Mostly adding the distribution text to the start of files, with a few additional comments added too.

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