Trait std::rand::Rng [] [src]

pub trait Rng {
    fn next_u32(&mut self) -> u32;

    fn next_u64(&mut self) -> u64 { ... }
    fn next_f32(&mut self) -> f32 { ... }
    fn next_f64(&mut self) -> f64 { ... }
    fn fill_bytes(&mut self, dest: &mut [u8]) { ... }
    fn gen<T>(&mut self) -> T where T: Rand { ... }
    fn gen_iter<T>(&'a mut self) -> Generator<'a, T, Self> where T: Rand { ... }
    fn gen_range<T>(&mut self, low: T, high: T) -> T where T: SampleRange + PartialOrd<T> { ... }
    fn gen_weighted_bool(&mut self, n: usize) -> bool { ... }
    fn gen_ascii_chars(&'a mut self) -> AsciiGenerator<'a, Self> { ... }
    fn choose<T>(&mut self, values: &'a [T]) -> Option<&'a T> { ... }
    fn shuffle<T>(&mut self, values: &mut [T]) { ... }
}
Unstable (rand)

: use rand from crates.io

A random number generator.

Required Methods

fn next_u32(&mut self) -> u32

Unstable (rand)

: use rand from crates.io

Return the next random u32.

This rarely needs to be called directly, prefer r.gen() to r.next_u32().

Provided Methods

fn next_u64(&mut self) -> u64

Unstable (rand)

: use rand from crates.io

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 next_f32(&mut self) -> f32

Unstable (rand)

: use rand from crates.io

Return the next random f32 selected from the half-open interval [0, 1).

By default this is implemented in terms of next_u32, but a random number generator which can generate numbers satisfying the requirements directly can overload this for performance. It is required that the return value lies in [0, 1).

See Closed01 for the closed interval [0,1], and Open01 for the open interval (0,1).

fn next_f64(&mut self) -> f64

Unstable (rand)

: use rand from crates.io

Return the next random f64 selected from the half-open interval [0, 1).

By default this is implemented in terms of next_u64, but a random number generator which can generate numbers satisfying the requirements directly can overload this for performance. It is required that the return value lies in [0, 1).

See Closed01 for the closed interval [0,1], and Open01 for the open interval (0,1).

fn fill_bytes(&mut self, dest: &mut [u8])

Unstable (rand)

: use rand from crates.io

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 panic if this is impossible (e.g. reading past the end of a file that is being used as the source of randomness).

fn gen<T>(&mut self) -> T where T: Rand

Unstable (rand)

: use rand from crates.io

Return a random value of a Rand type.

fn gen_iter<T>(&'a mut self) -> Generator<'a, T, Self> where T: Rand

Unstable (rand)

: use rand from crates.io

Return an iterator that will yield an infinite number of randomly generated items.

fn gen_range<T>(&mut self, low: T, high: T) -> T where T: SampleRange + PartialOrd<T>

Unstable (rand)

: use rand from crates.io

Generate a random value in the range [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.

Panics

Panics if low >= high.

fn gen_weighted_bool(&mut self, n: usize) -> bool

Unstable (rand)

: use rand from crates.io

Return a bool with a 1 in n chance of true

fn gen_ascii_chars(&'a mut self) -> AsciiGenerator<'a, Self>

Unstable (rand)

: use rand from crates.io

Return an iterator of random characters from the set A-Z,a-z,0-9.

fn choose<T>(&mut self, values: &'a [T]) -> Option<&'a T>

Unstable (rand)

: use rand from crates.io

Return a random element from values.

Return None if values is empty.

fn shuffle<T>(&mut self, values: &mut [T])

Unstable (rand)

: use rand from crates.io

Shuffle a mutable slice in place.

Implementors