Hare has support for generating pseudorandom numbers.
use fmt;
use math::random;
export fn main() void = {
// Set a seed value and initialize the random state with that
// seed. Since the seed value is constant all random numbers
// will end up being the same no matter how many times you run
// these examples.
const seed: u64 = 0xdeadbeef;
const rand = random::init(seed);
// We can get the next number from the random state.
let num = random::next(&rand);
fmt::println(num)!;
// Calling next again will generate another random number.
num = random::next(&rand);
fmt::println(num)!;
// We can also request a number with an upper bound.
num = random::u64n(&rand, 10);
fmt::println(num)!;
// We can even get a 64-bit float value as well.
let fnum = random::f64rand(&rand);
fmt::println(fnum)!;
};
$ hare run random-numbers.ha
5395234354446855067
12431030666585794594
7
0.919617363394144
Back to table of contents