Struct rand::XorShiftRng
pub struct XorShiftRng { // some fields omitted }
An Xorshift[1] random number generator.
The Xorshift algorithm is not suitable for cryptographic purposes
but is very fast. If you do not know for sure that it fits your
requirements, use a more secure one such as IsaacRng
or OSRng
.
[1]: Marsaglia, George (July 2003). "Xorshift RNGs". Journal of Statistical Software. Vol. 8 (Issue 14).
Methods
impl XorShiftRng
fn new() -> XorShiftRng
Create an xor shift random number generator with a random seed.
Trait Implementations
impl Rng for XorShiftRng
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 SeedableRng<[u32, ..4]> for XorShiftRng
fn reseed(&mut self, seed: [u32, ..4])
Reseed an XorShiftRng. This will fail if seed
is entirely 0.
fn from_seed(seed: [u32, ..4]) -> XorShiftRng
Create a new XorShiftRng. This will fail if seed
is entirely 0.