source: tags/release-1.0.5/src/Utils/get_random_spectrum.cc @ 1455

Last change on this file since 1455 was 146, checked in by Matthew Whiting, 18 years ago

A few new things:

  • Made a mycpgplot.hh file, to keep PGPlot-related constants (enum types)

in a standard namespace (so one can do cpgsci(RED) rather than cpgsci(2)...).
Incorporated this into all code that uses pgplot.

  • Improved the outputting of the number of detected objects in the search

functions. Now shows how many have been detected, before they are merged
into the list.

  • Fixed a bug in columns.cc that was incorrectly updating the precision for

negative velocities.

  • Additional text in the Guide, and general clean up of code.
File size: 1.8 KB
Line 
1#include <iostream>
2#include <stdlib.h>
3#include <time.h>
4#include <math.h>
5
6float getNormalRV()
7{
8  float v1,v2,s;
9  // simulate a standard normal RV via polar method
10  do{
11    v1 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
12    v2 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
13    s = v1*v1+v2*v2;
14  }while(s>1);
15  return sqrt(-2.*log(s)/s)*v1;
16
17}
18
19float getNormalRVtrunc()
20{
21  float v1,v2,s;
22  // simulate a standard normal RV via polar method
23  do{
24    v1 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
25    v2 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
26    s = v1*v1+v2*v2;
27  }while((s>1)||(fabs(sqrt(-2.*log(s)/s)*v1)>sqrt(2*M_LN2)));
28  return sqrt(-2.*log(s)/s)*v1;
29
30}
31
32float getNormalRV(float mean, float sigma)
33{
34  // simulate a standard normal RV via polar method
35  float v1,v2,s;
36  do{
37    v1 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
38    v2 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
39    s = v1*v1+v2*v2;
40  }while(s>1);
41  float z=sqrt(-2.*log(s)/s)*v1;
42  return z*sigma + mean;
43}
44
45void getRandomSpectrum(int length, float *x, double *y)
46{
47  srandom(time(0));
48  rand();
49  for(int i=0;i<length;i++){
50    x[i] = (float)i;
51    // simulate a standard normal RV via polar method
52    double v1,v2,s;
53    do{
54      v1 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
55      v2 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
56      s = v1*v1+v2*v2;
57    }while(s>1);
58    y[i] = sqrt(-2.*log(s)/s)*v1;
59  }
60}
61
62
63void getRandomSpectrum(int length, float mean, float sigma,
64                       float *x, double *y)
65{
66  srandom(time(0));
67  rand();
68  for(int i=0;i<length;i++){
69    x[i] = (float)i;
70    // simulate a standard normal RV via polar method
71    double v1,v2,s;
72    do{
73      v1 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
74      v2 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
75      s = v1*v1+v2*v2;
76    }while(s>1);
77    float z = sqrt(-2.*log(s)/s)*v1;
78    y[i] = z * sigma + mean;
79  }
80}
81
Note: See TracBrowser for help on using the repository browser.