phpOMS/Utils/RnG/Number.php
2024-01-26 22:54:00 +00:00

52 lines
1.1 KiB
PHP

<?php
/**
* Jingga
*
* PHP Version 8.1
*
* @package phpOMS\Utils\RnG
* @copyright Dennis Eichhorn
* @license OMS License 2.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
namespace phpOMS\Utils\RnG;
/**
* Number generator.
*
* @package phpOMS\Utils\RnG
* @license OMS License 2.0
* @link https://jingga.app
* @since 1.0.0
*/
final class Number
{
public static function normalDistributedRand(int $min, int $max) : int
{
$u1 = \mt_rand(1, 100) / 100;
$u2 = \mt_rand(1, 100) / 100;
// Box-Muller transform
$z = \sqrt(-2 * \log($u1)) * \cos(2 * \M_PI * $u2);
return (int) \max($min, \min($max, \round(($z + 3) / 6 * ($max - $min) + $min)));
}
/**
* For values [0; 100] a lambda of around 0.2 is recommended
*/
public static function exponentialDistributedRand(int $min, int $max, float $lambda) : int
{
$u = \mt_rand(1, 100) / 100;
$randomVariable = -$lambda * \log($u);
$randomValue = $min + ($max - $min) * $randomVariable;
return (int) \max($min, \min($max, $randomValue));
}
}