-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandom.cpp
More file actions
73 lines (58 loc) · 1.21 KB
/
Random.cpp
File metadata and controls
73 lines (58 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "Random.h"
#include "Gauss.h"
#include "FunzioneBase.h"
#include <cmath>
using namespace std;
Random::Random() {
m_a=1664525;
m_c=1013904223;
m_m=pow(2,31);
m_seed=0;
}
Random::Random(unsigned int seed) {
m_a=1664525;
m_c=1013904223;
m_m=pow(2,31);
m_seed=seed;
}
Random::~Random() { }
void Random::SetA(unsigned int a) {m_a=a;}
void Random::SetC(unsigned int c) {m_c=c;}
void Random::SetM(unsigned int m) {m_m=m;}
//Linear congruential generator
double Random::Rand() {
unsigned int n;
n=(m_a*m_seed+m_c)%m_m;
double d;
d=(n*1.)/(m_m-1);
m_seed=n;
return d;
}
//boxmuller method
double Random::BoxMuller(double mu, double sigma) { //mu=media
double s=this->Rand();
double t=this->Rand();
double g=sqrt(-2.*log(s))*cos(2*M_PI*t);
return mu+sigma*g;
}
//exponential random generator
double Random::Exp(double rate) {
double s=Rand();
return -rate*log(s);
}
//accept reject method
double Random::AcceptReject(double n, double mu, double sigma) { //numero sigma, valore atteso, sigma
Gauss G(mu,sigma);
double b=mu+sigma*n;
double a=mu-sigma*n;
double x;
double y;
do {
double s=Rand();
double t=Rand();
double max=G.Eval(mu);
y=max*t;
x=a+(b-a)*s;}
while(y>G.Eval(x));
return x;
}