/
TruncatedGaussian.pm6
56 lines (41 loc) · 1.19 KB
/
TruncatedGaussian.pm6
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
class RandomStratifiers
{
has $.cmax, $lambda;
has $.phifunc;
has $b;
method BUILD(:$c) {
.cmax = $c;
.lambda = ($cmax + sqrt($cmax * $cmax + 4)) / 2
.phifunc = phifunc;
.b = exp((.lambda * .lambda - 2 * .lambda * cmax) /2) /
sqrt(2 * PI) * .lambda * (1 - .phifunc(c));
}
method reinit($c) {
.cmax = $c;
.lambda = ($cmax + sqrt($cmax * $cmax + 4)) / 2;
.b = exp((.lambda * .lambda - 2 * .lambda * cmax) /2) /
sqrt(2 * PI) * .lambda * (1 - .phifunc(c));
}
method phifunc ($x) {
}
method exponentialdistribution($x) {
return (.b * .lambda * exp(- .lambda * $x));
}
method phi_x_plus_c($x) { ### phi (x + c)
return self.exponentialdistribution($x) * (1 - .phifunc(.cmax));
}
method chance($x, $c) {
self.reinit($c);
return self.phi_x_plus_c($x);
}
method gen() { ### generate a truncated Gaussian random probability
### which is exponential and uniform distributed
### This can be put in a population/distribution array
### which sets a fractal landscape to use at init
### and runtime of the AI in your game (random behaviour)
return self.chance(0..1000000.rand, 0);
}
method phifunc ($x) {
return $x;
}
}