-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNormalDistribution.php
41 lines (33 loc) · 1.5 KB
/
NormalDistribution.php
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
<?php
require_once __DIR__ . '/Distribution.php';
// ============================================================================
class NormalDistribution extends Distribution {
/*
Generates normally distributed values in [min, max] ranged from a random
number in [0, 1] with a default mean of 0.5 and a default std. dev. of 0.1
*/
// ============================================================================
// ------------------------------------------------------------------------
protected function createGenerator(
) {
// ------------------------------------------------------------------------
// we use these parameters, which creates a nice spectrum of values between 0 and 1
return new MathPHP\Probability\Distribution\Continuous\Normal(
isset($this->options['mean']) ? $this->options['mean'] : .5,
isset($this->options['std_dev']) ? $this->options['std_dev'] : .1
);
}
// ------------------------------------------------------------------------
public function getRandomValue(
$decimals = 0
) {
// ------------------------------------------------------------------------
$val = $this->generator->rand();
// respect bounds [0, 1]
$val = max(min(1., $val), 0.);
// now get corresponding from min/max setting
$val = $this->min + $val * ($this->max - $this->min);
$val = round($val, $decimals);
return $decimals === 0 ? intval($val) : $val;
}
}