Crate rand
Utilities for 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::<f64>()
.
See the distributions
submodule for sampling random numbers from
distributions like normal and exponential.
Task-local RNG
There is built-in support for a RNG associated with each task stored
in task-local storage. This RNG can be accessed via task_rng
, or
used implicitly via random
. This RNG is normally randomly seeded
from an operating-system source of randomness, e.g. /dev/urandom
on
Unix systems, and will automatically reseed itself from this source
after generating 32 KiB of random data.
Cryptographic security
An application that requires random numbers for cryptographic purposes
should prefer OSRng
, which reads randomness from one of the source
that the operating system provides (e.g. /dev/urandom
on
Unixes). The other random number generators provided by this module
are either known to be insecure (XorShiftRng
), or are not verified
to be secure (IsaacRng
, Isaac64Rng
and StdRng
).
Note: on Linux, /dev/random
is more secure than /dev/urandom
,
but it is a blocking RNG, and will wait until it has determined that
it has collected enough entropy to fulfill a request for random
data. It can be used with the Rng
trait provided by this module by
opening the file and passing it to reader::ReaderRng
. Since it
blocks, /dev/random
should only be used to retrieve small amounts of
randomness.
Examples
use rand::Rng; let mut rng = rand::task_rng(); if rng.gen() { // bool println!("int: {}, uint: {}", rng.gen::<int>(), rng.gen::<uint>()) }
let tuple_ptr = rand::random::<~(f64, char)>(); println!("{:?}", tuple_ptr)
Reexports
pub use isaac::{IsaacRng, Isaac64Rng}; |
Modules
distributions | Sampling from random distributions. |
isaac | The ISAAC random number generator. |
os | Interfaces to the operating system provided random number generators. |
reader | A wrapper around any Reader to treat it as an RNG. |
reseeding | A wrapper around another RNG that reseeds it after it generates a certain number of random bytes. |
Structs
Closed01 | A wrapper for generating floating point numbers uniformly in the
closed interval |
OSRng | A random number generator that retrieves randomness straight from the operating system. Platform sources: |
Open01 | A wrapper for generating floating point numbers uniformly in the
open interval |
StdRng | The standard RNG. This is designed to be efficient on the current platform. |
TaskRng | The task-local RNG. |
XorShiftRng | An Xorshift[1] random number generator. |
Traits
Rand | A type that can be randomly generated using an |
Rng | A random number generator. |
SeedableRng | A random number generator that can be explicitly seeded to produce the same stream of randomness multiple times. |
Functions
random | Generate a random value using the task-local random number generator. |
rng | Create a random number generator with a default algorithm and seed. |
task_rng | Retrieve the lazily-initialized task-local random number
generator, seeded by the system. Intended to be used in method
chaining style, e.g. |
weak_rng | Create a weak random number generator with a default algorithm and seed. |