diff --git a/README.md b/README.md index 0a8fdd7..b5e0934 100644 --- a/README.md +++ b/README.md @@ -444,8 +444,9 @@ floor | `(floor 2.5)` | `2` | Get the next lowest integer. ceil | `(ceil 2.5)` | `3` | Get the next highest integer. pow | `(pow 2 4)` | `16` | Raise the first argument to the power of the second argument. sqrt | `(sqrt 2)` | `1.41` | Calculate the square root. -coinflip | `(coinflip)` | `true` | Return true or false with equal probability. rand | `(rand 5 10)` | `8` | Return a random integer between given min and max values. +randf | `(randf)` | `0.678` | Return a random float between 0 (inclusive) and 1 (exclusive). +rand-seed | `(rand-seed 256)` | `256` | Seed the random number generator with the given value. ### Regular expression functions diff --git a/src/Lib/Math.php b/src/Lib/Math.php index 06aa9bf..a3c4643 100644 --- a/src/Lib/Math.php +++ b/src/Lib/Math.php @@ -122,12 +122,19 @@ class Math implements ILib // Random number generator - $env->set('coinflip', new CoreFunc('coinflip', 'Return true or false with equal probability.', 0, 0, - fn () => boolval(mt_rand(0, 1)) - )); - $env->set('rand', new CoreFunc('rand', 'Return a random integer between given min and max values.', 2, 2, fn ($min, $max) => mt_rand($min, $max) )); + + $env->set('randf', new CoreFunc('randf', 'Return a random float between given 0 (inclusive) and 1 (exclusive).', 0, 0, + fn () => mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax() + )); + + $env->set('rand-seed', new CoreFunc('rand-seed', 'Seed the random number generator with the given value.', 1, 1, + function (int $a) { + mt_srand($a); + return $a; + } + )); } }