Conditions | 2 |
Paths | 1 |
Total Lines | 16 |
Code Lines | 9 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
47 | function generate_gaussian_noise(float $mu, float $sigma): array |
||
48 | { |
||
49 | static $twoPi = 2 * M_PI; |
||
50 | |||
51 | // create two random numbers, make sure u1 is greater than epsilon |
||
52 | do { |
||
53 | $u1 = (float) mt_rand() / (float) mt_getrandmax(); |
||
54 | $u2 = (float) mt_rand() / (float) mt_getrandmax(); |
||
55 | } while ($u1 < PHP_FLOAT_EPSILON); |
||
56 | |||
57 | // compute z0 and z1 |
||
58 | $mag = $sigma * sqrt(-2.0 * log($u1)); |
||
59 | $z0 = $mag * cos($twoPi * $u2) + $mu; |
||
60 | $z1 = $mag * sin($twoPi * $u2) + $mu; |
||
61 | |||
62 | return [$z0, $z1]; |
||
63 | } |
||
64 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: