random¶
A seedable pseudo-random generator for ints and floats.
The generator is a PCG32 instance shared by the whole program. On the first call
that draws a number it seeds itself from the clock, so a run without an explicit
seed differs each time. Call seed to make a run repeatable.
The sequence is not suitable for cryptography.
The module binds a small dynamic library, so it is one of the three std modules
that need that library present at run time. A .cdlb built from a program that
imports std/random records the binding by name and re-opens it when the
artifact runs; see artifacts.
seed¶
s: an int.- Returns: nothing.
Restarts the generator from s. Two runs seeded with the same value draw the
same sequence.
random_int¶
- Returns: an int drawn from the generator's full range, so the result can be negative.
random_int_range¶
min,max: ints.- Returns: an int between
minandmax, with both ends included.
import "std/random" as random;
fn main() {
random::seed(42);
print(random::random_int_range(1, 6));
}
random¶
- Returns: a float in the half-open range from 0 up to 1.
random_range¶
min,max: floats.- Returns: a float between
minandmax, includingminand excludingmax.