Struct urandom::Random

source ·
pub struct Random<R: ?Sized>(pub R);
Expand description

Rich interface for consuming random number generators.

Tuple Fields

0: R

Implementations

Returns the next u32 in the sequence.

Examples
let value = urandom::new().next_u32();

Returns the next u64 in the sequence.

Examples
let value = urandom::new().next_u64();

Returns a uniform random f32 in the half-open interval [1.0, 2.0).

As only 23 bits are necessary to construct a random float in this range, implementations may override this method to provide a more efficient implementation.

If high quality uniform random floats are desired in open interval (0.0, 1.0) without bias see the Float01 distribution.

Examples
let value = urandom::new().next_f32();
assert!(value >= 1.0 && value < 2.0);

Returns a uniform random f64 in the half-open interval [1.0, 2.0).

As only 52 bits are necessary to construct a random double in this range, implementations may override this method to provide a more efficient implementation.

If high quality uniform random floats are desired in open interval (0.0, 1.0) without bias see the Float01 distribution.

Examples
let value = urandom::new().next_f64();
assert!(value >= 1.0 && value < 2.0);

Fills the destination buffer with random values from the Rng.

The underlying Rng may implement this as efficiently as possible and may not be the same as simply filling with next_u32.

Examples
let mut rng = urandom::new();
let mut buffer = [0u32; 32];
rng.fill_u32(&mut buffer);
assert_ne!(buffer, [0; 32]);

Fills the destination buffer with uniform random values from the Rng.

The underlying Rng may implement this as efficiently as possible and may not be the same as simply filling with next_u64.

Examples
let mut rng = urandom::new();
let mut buffer = [0u64; 32];
rng.fill_u64(&mut buffer);
assert_ne!(buffer, [0; 32]);

Fills the destination buffer with uniform random bytes from the Rng.

The underlying Rng may implement this as efficiently as possible.

Examples
let mut rng = urandom::new();
let mut buffer = [0u8; 32];
rng.fill_bytes(&mut buffer);
assert_ne!(buffer, [0u8; 32]);

Advances the internal state significantly.

Useful to produce deterministic independent random number generators for parallel computation.

Clones the current instance and advances the internal state significantly.

Useful to produce deterministic independent random number generators for parallel computation.

Examples
let mut rng = urandom::new();
for _ in 0..10 {
	parallel_computation(rng.split());
}

Returns a sample from the Standard distribution.

Examples
let int: i8 = urandom::new().next();

Fills the given slice with samples from the Standard distribution.

Because of its generic nature no optimizations are applied and all values are sampled individually from the distribution.

Examples
let mut rng = urandom::new();
let mut buffer = [false; 32];
rng.fill(&mut buffer);

Returns a sample from the Uniform distribution within the given interval.

Examples
let eyes = urandom::new().range(1..=6);
assert!(eyes >= 1 && eyes <= 6);

If more than one sample from a specific interval is desired, it is more efficient to reuse the uniform sampler.

let mut rng = urandom::new();
let distr = urandom::distributions::Uniform::from(0..100);

loop {
	let value = rng.sample(&distr);
	assert!(value >= 0 && value < 100);
	if value == 0 {
		break;
	}
}

Returns a sample from the given distribution.

See the distributions documentation for a list of available distributions.

Returns an iterator of samples from the given distribution.

See the distributions documentation for a list of available distributions.

Returns true with the given probability.

This is known as the Bernoulli distribution.

Precision

For p >= 1.0, the resulting distribution will always generate true.
For p <= 0.0, the resulting distribution will always generate false.

Flips a coin.

Returns true when heads and false when tails with 50% probability for either result.

Simply an alias for rng.next::<bool>() but describes the intent of the caller.

Returns a random sample from the collection.

Returns None if and only if the collection is empty.

This method uses Iterator::size_hint for optimisation. With an accurate hint and where Iterator::nth is a constant-time operation this method can offer O(1) performance.

For slices, prefer choose which guarantees O(1) performance.

Examples

Sample a random fizz, buzz or fizzbuzz number up to 100:

fn is_fizzbuzz(n: &i32) -> bool {
	n % 3 == 0 || n % 5 == 0
}

let mut rng = urandom::new();
let fizzbuzz = rng.single((0..100).filter(is_fizzbuzz)).unwrap();
assert!(fizzbuzz % 3 == 0 || fizzbuzz % 5 == 0);

Pick a random emoji:

let mood = urandom::new().single("😀😎😐😕😠😢".chars()).unwrap();
println!("I am {}!", mood);

Collect random samples from the collection into the buffer until it is filled.

Although the elements are selected randomly, the order of elements in the buffer is neither stable nor fully random. If random ordering is desired, shuffle the result.

Returns the number of elements added to the buffer. This equals the length of the buffer unless the iterator contains insufficient elements, in which case this equals the number of elements available.

Complexity is O(n) where n is the size of the collection.

Returns a random usize in the [0, len) interval, mostly.

If the len is zero an arbitrary value is returned directly from the Rng. When used with indexing the bounds check should fail. Do not assume this value is inbounds.

Examples
let mut rng = urandom::new();
for len in 1..12345 {
	let index = rng.index(len);
	assert!(index < len, "len:{} index:{} was not inbounds", len, index);
}

Returns a shared reference to one random element of the slice, or None if the slice is empty.

Returns a unique reference to one random element of the slice, or None if the slice is empty.

Returns an iterator over random chosen elements of the slice with repetition.

Produces None values if the slice is empty.

Standard Fisher–Yates shuffle.

Examples
let mut rng = urandom::new();
let mut array = [1, 2, 3, 4, 5];
println!("Unshuffled: {:?}", array);
rng.shuffle(&mut array);
println!("Shuffled:   {:?}", array);

Shuffle only the first n elements.

This is an efficient method to select n elements at random from the slice without repetition, provided the slice may be mutated.

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
Read all bytes until EOF in this source, placing them into buf. Read more
Read all bytes until EOF in this source, appending them to buf. Read more
Read the exact number of bytes required to fill buf. Read more
Like read, except that it reads into a slice of buffers. Read more
🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
🔬This is a nightly-only experimental API. (read_buf)
Read the exact number of bytes required to fill cursor. Read more
Creates a “by reference” adaptor for this instance of Read. Read more
Transforms this Read instance to an Iterator over its bytes. Read more
Creates an adapter which will chain this stream with another. Read more
Creates an adapter which will read at most limit bytes from it. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.