Struct rand::isaac::Isaac64Rng
pub struct Isaac64Rng { // some fields omitted }
A random number generator that uses ISAAC-64[1], the 64-bit variant of the ISAAC algorithm.
The ISAAC algorithm is generally accepted as suitable for
cryptographic purposes, but this implementation has not be
verified as such. Prefer a generator like OSRng
that defers to
the operating system for cases that need high security.
[1]: Bob Jenkins, ISAAC: A fast cryptographic random number generator
Methods
impl Isaac64Rng
fn new() -> Isaac64Rng
Create a 64-bit ISAAC random number generator with a random seed.
fn new_unseeded() -> Isaac64Rng
Create a 64-bit ISAAC random number generator using the default fixed seed.
Trait Implementations
impl Rng for Isaac64Rng
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 [u64]> for Isaac64Rng
fn reseed(&mut self, seed: &'a [u64])
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 [u64]) -> Isaac64Rng
Create an ISAAC random number generator with a seed. This can be any length, although the maximum number of elements used is 256 and any more will be silently ignored. A generator constructed with a given seed will generate the same sequence of values as all other generators constructed with that seed.