Created
April 2, 2026 21:07
-
-
Save ghostwriter/adca2a52b002ff1661535fd732e9ac93 to your computer and use it in GitHub Desktop.
GaussGenerator in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| declare(strict_types=1); | |
| /** | |
| * #BlackLivesMatter - github.com/ghostwriter | |
| **/ | |
| use Random\Engine\Mt19937; | |
| use Random\Randomizer; | |
| final class GaussGenerator | |
| { | |
| public function __construct( | |
| private readonly Randomizer $random = new Randomizer() | |
| ) {} | |
| public function gauss(float $mu = 0.0, float $sigma = 1.0): float | |
| { | |
| static $next; | |
| if ($next !== null) { | |
| $z = $next; | |
| $next = null; | |
| } else { | |
| $x2pi = $this->random->getFloat(0.0, 1.0) * 2.0 * M_PI; | |
| $g2rad = sqrt(-2.0 * log(1.0 - $this->random->getFloat(0.0, 1.0))); | |
| $z = cos($x2pi) * $g2rad; | |
| $next = sin($x2pi) * $g2rad; | |
| } | |
| return $mu + $z * $sigma; | |
| } | |
| } |
ghostwriter
commented
Apr 2, 2026
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment