Skip to content
CryoCryo home
Stdlibrandom

source

import std::random::source; · source

Types

RandomSource

type trait RandomSource {
    next_64(mut &this) -> u64;
    next<T>(mut &this) -> T;
    next_below(mut &this, bound: u64) -> u64;
    next_range<T>(mut &this, lo: T, hi: T) -> T;
    next_below_wide(mut &this, bound: u128) -> u128;
    fill(mut &this, dst: Slice<u8>) -> void;
    shuffle<T>(mut &this, items: &Slice<T>) -> void;
    choose<T>(mut &this, items: &Slice<T>) -> Option<T*>;
}

One required primitive — next_64 — and a substantial set of defaults built on it.

Methods

next_64(mut &this) -> u64;

The single required primitive: the next 64 random bits.

next<T>(mut &this) -> T;

A uniform draw of type T, dispatched at compile time via static match (T) over the primitive next_64. Covers every fixed-width integer, the platform-width usize/isize, boolean, f32/f64, and char: - integers - the required number of bits, taken from the top (best-mixed) end for the sub-64-bit widths; u128/i128 concatenate two draws. - boolean - the top (highest-quality) bit. - f64/f32 - a uniform value in [0.0, 1.0); f64 uses the top 53 bits so every representable value in the interval is equally likely. - char - a uniform Unicode scalar value (the surrogate ra

next_below(mut &this, bound: u64) -> u64;

A uniform integer in [0, bound). Unbiased: the top partial bucket is rejected so the modulo introduces no skew. Panics when bound == 0 - an empty range has no value to return.

next_range<T>(mut &this, lo: T, hi: T) -> T;

A uniform integer of type T in [lo, hi). Requires lo < hi. static match (T) covers every integer width: the spans of the 64-bit-and-narrower types are drawn unbiased through next_below; u128/i128 use an inline 128-bit rejection sampler.

next_below_wide(mut &this, bound: u128) -> u128;

A uniform u128 in [0, bound), unbiased by the same top-bucket rejection as next_below but over the full 128-bit range. Backs the u128/i128 arms of next_range.

fill(mut &this, dst: Slice<u8>) -> void;

Fill dst with random bytes, writing through the view.

shuffle<T>(mut &this, items: &Slice<T>) -> void;

Shuffle items in place with a uniform Fisher-Yates pass. Works for any element type - elements are swapped, never copied out, so no Copy bound is needed.

choose<T>(mut &this, items: &Slice<T>) -> Option<T*>;

Borrow a uniformly chosen element of items, or None when items is empty. Returns a pointer in place, so no Copy is required and owning element types are fine.

next<T>()

A uniform draw of type T, dispatched at compile time via static match (T). Select the type with a turbofish (r.next::<u32>()) or let context infer it.

TWhat you get
Any fixed-width integer, usize, isizeThe required bits, taken from the top (best-mixed) end. u128 and i128 concatenate two draws.
booleanThe top bit.
f64 / f32A uniform value in [0.0, 1.0). f64 uses the top 53 bits, so every representable value in the interval is equally likely.
charA uniform Unicode scalar; the surrogate range is skipped.

Bounded draws

  • next_below(bound) — uniform in [0, bound). Unbiased: the top partial bucket is rejected so the modulo introduces no skew. Panics when bound == 0, since an empty range has no value to return.
  • next_range<T>(lo, hi) — uniform in [lo, hi), requiring lo < hi. Every integer width is covered; 64-bit-and-narrower spans go through next_below, and u128/i128 use an inline 128-bit rejection sampler.
  • next_below_wide(bound) — the same top-bucket rejection over the full 128-bit range.

Bulk and collections

  • fill(dst) — fill a byte view.
  • shuffle<T>(items) — an in-place uniform Fisher-Yates pass. Elements are swapped, never copied out, so there is no Copy bound and owning element types work.
  • choose<T>(items) — a uniformly chosen element, borrowed in place. None when empty.

Implementors

implement trait RandomSource for struct Rng
std::random::rng
implement trait RandomSource for struct SecureRng
std::random::secure

Constants

const U64_MAX: u64 = 0xFFFFFFFFFFFFFFFF;