remove coinflip, add randf, rand-seed

This commit is contained in:
Pekka Laiho 2020-12-10 20:55:14 +07:00
parent 458d09c007
commit 9d2de20c1e
2 changed files with 13 additions and 5 deletions

View File

@ -444,8 +444,9 @@ floor | `(floor 2.5)` | `2` | Get the next lowest integer.
ceil | `(ceil 2.5)` | `3` | Get the next highest 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. 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. 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. 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 ### Regular expression functions

View File

@ -122,12 +122,19 @@ class Math implements ILib
// Random number generator // 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, $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) 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;
}
));
} }
} }