Struct rand::StdRng
pub struct StdRng { // some fields omitted }
The standard RNG. This is designed to be efficient on the current platform.
Methods
impl StdRng
fn new() -> StdRng
Create a randomly seeded instance of StdRng
.
This is a very expensive operation as it has to read
randomness from the operating system and use this in an
expensive seeding operation. If one is only generating a small
number of random numbers, or doesn't need the utmost speed for
generating each number, task_rng
and/or random
may be more
appropriate.
Trait Implementations
impl Rng for StdRng
fn next_u32(&mut self) -> u32
Return the next random u32.
This rarely needs to be called directly, prefer r.gen()
to
r.next_u32()
.
fn next_u64(&mut self) -> u64
Return the next random u64.
By default this is implemented in terms of next_u32
. An
implementation of this trait must provide at least one of
these two methods. Similarly to next_u32
, this rarely needs
to be called directly, prefer r.gen()
to r.next_u64()
.
fn fill_bytes(&mut self, dest: &mut [u8])
Fill dest
with random data.
This has a default implementation in terms of next_u64
and
next_u32
, but should be overridden by implementations that
offer a more efficient solution than just calling those
methods repeatedly.
This method does not have a requirement to bear any fixed
relationship to the other methods, for example, it does not
have to result in the same output as progressively filling
dest
with self.gen::<u8>()
, and any such behaviour should
not be relied upon.
This method should guarantee that dest
is entirely filled
with new data, and may fail if this is impossible
(e.g. reading past the end of a file that is being used as the
source of randomness).
Example
use rand::{task_rng, Rng}; let mut v = [0u8, .. 13579]; task_rng().fill_bytes(v); println!("{:?}", v);
fn gen<T: Rand>(&mut self) -> T
Return a random value of a Rand
type.
Example
use rand::{task_rng, Rng}; let mut rng = task_rng(); let x: uint = rng.gen(); println!("{}", x); println!("{:?}", rng.gen::<(f64, bool)>());
fn gen_vec<T: Rand>(&mut self, len: uint) -> ~[T]
Return a random vector of the specified length.
Example
use rand::{task_rng, Rng}; let mut rng = task_rng(); let x: ~[uint] = rng.gen_vec(10); println!("{:?}", x); println!("{:?}", rng.gen_vec::<(f64, bool)>(5));
fn gen_range<T: Ord + SampleRange>(&mut self, low: T, high: T) -> T
Generate a random value in the range [low
, high
). Fails if
low >= high
.
This is a convenience wrapper around
distributions::Range
. If this function will be called
repeatedly with the same arguments, one should use Range
, as
that will amortize the computations that allow for perfect
uniformity, as they only happen on initialization.
Example
use rand::{task_rng, Rng}; let mut rng = task_rng(); let n: uint = rng.gen_range(0u, 10); println!("{}", n); let m: f64 = rng.gen_range(-40.0, 1.3e5); println!("{}", m);
fn gen_weighted_bool(&mut self, n: uint) -> bool
Return a bool with a 1 in n chance of true
Example
use rand::{task_rng, Rng}; let mut rng = task_rng(); println!("{:b}", rng.gen_weighted_bool(3));
fn gen_ascii_str(&mut self, len: uint) -> ~str
Return a random string of the specified length composed of A-Z,a-z,0-9.
Example
use rand::{task_rng, Rng}; println!("{}", task_rng().gen_ascii_str(10));
fn choose<T: Clone>(&mut self, values: &[T]) -> T
Choose an item randomly, failing if values
is empty.
fn choose_option<'a, T>(&mut self, values: &'a [T]) -> Option<&'a T>
Choose Some(&item)
randomly, returning None
if values is
empty.
Example
use rand::{task_rng, Rng}; let choices = [1, 2, 4, 8, 16, 32]; let mut rng = task_rng(); println!("{:?}", rng.choose_option(choices)); println!("{:?}", rng.choose_option(choices.slice_to(0)));
fn shuffle<T>(&mut self, values: ~[T]) -> ~[T]
fn shuffle_mut<T>(&mut self, values: &mut [T])
Shuffle a mutable vector in place.
Example
use rand::{task_rng, Rng}; let mut rng = task_rng(); let mut y = [1,2,3]; rng.shuffle_mut(y); println!("{:?}", y); rng.shuffle_mut(y); println!("{:?}", y);
fn sample<A, T: Iterator<A>>(&mut self, iter: T, n: uint) -> ~[A]
Randomly sample up to n
elements from an iterator.
Example
use rand::{task_rng, Rng}; let mut rng = task_rng(); let sample = rng.sample(range(1, 100), 5); println!("{:?}", sample);
impl<'a> SeedableRng<&'a [uint]> for StdRng
fn reseed(&mut self, seed: &'a [uint])
Reseed an RNG with the given seed.
Example
use rand::{Rng, SeedableRng, StdRng}; let mut rng: StdRng = SeedableRng::from_seed(&[1, 2, 3, 4]); println!("{}", rng.gen::<f64>()); rng.reseed([5, 6, 7, 8]); println!("{}", rng.gen::<f64>());
fn from_seed(seed: &'a [uint]) -> StdRng
Create a new RNG with the given seed.
Example
use rand::{Rng, SeedableRng, StdRng}; let mut rng: StdRng = SeedableRng::from_seed(&[1, 2, 3, 4]); println!("{}", rng.gen::<f64>());