Module fstd::rand

source ·
Expand description

Utilities for random number generation.

The implementation uses wyrand for now. It’s NOT cryptographically secure.

Examples

The public functions in rand is NOT reproducible due to possible replacement in future library versions.

For a reproducible generator, use a named PRNG struct, e.g. WyRng.

Generate a random u64:

let r = fstd::rand::u64(..);

Generate a random u64 in the [0,100):

let r = fstd::rand::u64(0..100);

Create a local PRNG with a random seed and generate a random u64:

use fstd::rand::WyRng;
let rng = WyRng::new();
let r = rng.u64(..);

Create a local PRNG with specific seed and generate a random u64, it is a reproducible generator.

use fstd::rand::WyRng;
let rng = WyRng::from_seed_u64(123);
let r = rng.u64(..);

Structs

A random number generator based on wyrand. It’s NOT cryptographically secure.

Functions

Returns a random f32 in the [0.0,1.0). (thread-safe, but not cryptographically secure)
Returns a random f64 in the [0.0,1.0). (thread-safe, but not cryptographically secure)
Returns a random i32 in the given range(thread-safe, but not cryptographically secure). Panics if the range is invalid.
Returns a random i64 in the given range(thread-safe, but not cryptographically secure). Panics if the range is invalid.
Returns a random isize in the given range(thread-safe, but not cryptographically secure). Panics if the range is invalid.
Returns a random u32 in the given range(thread-safe, but not cryptographically secure). Panics if the range is invalid.
Returns a random u64 in the given range(thread-safe, but not cryptographically secure). Panics if the range is invalid.
Returns a random usize in the given range(thread-safe, but not cryptographically secure). Panics if the range is invalid.