Random number generation.
The key functions are random()
and Rng::gen()
. These are polymorphic
and so can be used to generate any type that implements Rand
. Type inference
means that often a simple call to rand::random()
or rng.gen()
will
suffice, but sometimes an annotation is required, e.g. rand::random::<float>()
.
See the distributions
submodule for sampling random numbers from
distributions like normal and exponential.
use std::rand;
use std::rand::Rng;
fn main() {
let mut rng = rand::rng();
if rng.gen() { // bool
printfln!("int: %d, uint: %u", rng.gen(), rng.gen())
}
}
use std::rand;
fn main () {
let tuple_ptr = rand::random::<~(f64, char)>();
printfln!(tuple_ptr)
}
distributions | Sampling from random distributions |
IsaacRng | A random number generator that uses the ISAAC algorithm. |
Weighted | A value with a particular weight compared to other values |
XorShiftRng |
Rand | A type that can be randomly generated using an Rng |
Rng | A random number generator |
random | Returns a random value of a Rand type, using the task's random number generator. |
rng | Create a random number generator with a default algorithm and seed. |
seed | Create a new random seed. |
task_rng | Gives back a lazily initialized task-local random number generator,
seeded by the system. Intended to be used in method chaining style, ie
|
weak_rng | Create a weak random number generator with a default algorithm and seed. |
Prefix searches with a type followed by a colon (e.g.
fn:
) to restrict the search to a given type.
Accepted types are: fn
, mod
,
struct
(or str
), enum
,
trait
, typedef
(or
tdef
).