-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKTzV2_Maths_Random.cs
96 lines (83 loc) · 2.7 KB
/
KTzV2_Maths_Random.cs
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using MersenneTwister;
namespace KTzV2.Maths.Random
{
public class GaussianRand
{
private MT19937 rand { get; set; } // the random number
public Double stdDev { get; private set; }
public Double mean { get; private set; }
public Func<Double> getRandom { get; private set; }
public GaussianRand(Double mean, Double stdDev)
: this (mean, stdDev, Convert.ToUInt64(DateTime.Now.Ticks)) // seeding the random generator with the current time
{
//this.mean = mean;
//this.stdDev = stdDev;
//rand = new MT19937();
//rand.init_genrand(Convert.ToUInt64(DateTime.Now.Ticks)); // seeding the random generator with the current time
}
public GaussianRand(Double mean, Double stdDev, UInt64 seed)
{
this.mean = mean;
this.stdDev = stdDev;
rand = new MT19937();
rand.init_genrand(seed);
if (stdDev == 0.0D)
{
this.getRandom = this.getRandom0;
}
else
{
this.getRandom = this.getRandomG;
}
}
private Double getRandom0()
{
return this.mean;
}
private Double getRandomG()
{
Double v1;
Double v2;
Double R;
do
{
v1 = 2.0 * rand.genrand_real3() - 1.0;
v2 = 2.0 * rand.genrand_real3() - 1.0;
R = v1 * v1 + v2 * v2;
} while (R >= 1.0);
return this.mean + this.stdDev * v2 * Math.Sqrt(-2.0 * Math.Log(R) / R);
}
}
public class HomogeneousRand
{
private MT19937 rand;
//private Random rand;
//private Int32 seed;
public HomogeneousRand()
: this (Convert.ToUInt64(DateTime.Now.Ticks))
{
//seed = DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond;
//rand = new MT19937();
//rand.init_genrand(Convert.ToUInt64(DateTime.Now.Ticks)); // seeding the random generator with the current time
}
public HomogeneousRand(UInt64 seed)
{
rand = new MT19937();
rand.init_genrand(seed); // seeding the random generator with the current time
}
public Double GetRandomFull()
{
//rand = new Random(seed++);
return rand.genrand_real1();
}
public Double GetRandomLT1()
{
return rand.genrand_real2();
}
public Double GetRandomGT0LT1()
{
return rand.genrand_real3();
}
}
}