Skip to main content

RandomGenerator

Struct RandomGenerator 

Source
pub struct RandomGenerator<G: ByteRandomGenerator>(/* private fields */);
Expand description

A cryptographically secure random number generator.

This csprng is used by every objects that needs sampling in the library. If the proper instructions are available on the machine, it will use an hardware-accelerated variant for the generation. If not, a fallback software version will be used.

§Safe multithreaded use

When using a csprng in a multithreaded setting, it is important to make sure that the same sequence of bytes is not generated twice on two different threads. This csprng offers a simple way to ensure that: any generator can be forked into several bounded generators, which are able to sample a fixed number of bytes. This forking operation has the effect of shifting the state of the parent generator accordingly. This way, the children generators can be used by the different threads safely:

use tfhe::core_crypto::commons::math::random::RandomGenerator;
use tfhe_csprng::generators::SoftwareRandomGenerator;
use tfhe_csprng::seeders::Seed;
let mut generator = RandomGenerator::<SoftwareRandomGenerator>::new(Seed(0));
assert_eq!(generator.remaining_bytes(), None); // The generator is unbounded.
let children = generator
    .try_fork(5, 2) // 5 generators each able to generate 2 bytes.
    .unwrap()
    .collect::<Vec<_>>();
for child in children.into_iter() {
    assert_eq!(child.remaining_bytes(), Some(2));
    std::thread::spawn(move || {
        let child = child;
        // use the prng to generate 2 bytes.
        // ...
    });
}
// use the parent to generate as many bytes as needed.

Implementations§

Source§

impl<G: ByteRandomGenerator> RandomGenerator<G>

Source

pub fn generate_next(&mut self) -> u8

Source

pub fn new(params: impl Into<AesCtrParams>) -> Self

Generate a new generator, optionally seeding it with the given value.

§Example
use tfhe::core_crypto::commons::math::random::RandomGenerator;
use tfhe_csprng::generators::SoftwareRandomGenerator;
use tfhe_csprng::seeders::Seed;
let generator = RandomGenerator::<SoftwareRandomGenerator>::new(Seed(0));
Source

pub fn next_table_index(&self) -> Option<TableIndex>

Source

pub fn remaining_bytes(&self) -> Option<usize>

Return the number of bytes that can still be generated, if the generator is bounded.

§Example
use tfhe::core_crypto::commons::math::random::RandomGenerator;
use tfhe_csprng::generators::SoftwareRandomGenerator;
use tfhe_csprng::seeders::Seed;
let mut generator = RandomGenerator::<SoftwareRandomGenerator>::new(Seed(0));
assert_eq!(generator.remaining_bytes(), None);
let generator = generator.try_fork(1, 50).unwrap().next().unwrap();
assert_eq!(generator.remaining_bytes(), Some(50));
Source

pub fn try_fork( &mut self, n_child: usize, bytes_per_child: usize, ) -> Result<impl Iterator<Item = Self>, ForkError>

Tries to fork the current generator into n_child generator bounded to bytes_per_child. If n_child*bytes_per_child exceeds the bound of the current generator, the method returns None.

§Example
use tfhe::core_crypto::commons::math::random::RandomGenerator;
use tfhe_csprng::generators::SoftwareRandomGenerator;
use tfhe_csprng::seeders::Seed;
let mut generator = RandomGenerator::<SoftwareRandomGenerator>::new(Seed(0));
let children = generator.try_fork(5, 50).unwrap().collect::<Vec<_>>();
Source

pub fn random_from_distribution<Scalar, D>(&mut self, distribution: D) -> Scalar
where D: Distribution, Scalar: RandomGenerable<D>,

Generate a random scalar from the given distribution under the native modulus.

§Example
use tfhe::core_crypto::commons::math::random::{Gaussian, RandomGenerator, Uniform};
use tfhe_csprng::generators::SoftwareRandomGenerator;
use tfhe_csprng::seeders::Seed;

let mut generator = RandomGenerator::<SoftwareRandomGenerator>::new(Seed(0));

let random = generator.random_from_distribution::<u8, _>(Uniform);
let random = generator.random_from_distribution::<i8, _>(Uniform);
let random = generator.random_from_distribution::<u64, _>(Gaussian {
    mean: 0.0,
    std: 1.0,
});
Source

pub fn random_from_distribution_custom_mod<Scalar, D>( &mut self, distribution: D, custom_modulus: CiphertextModulus<Scalar>, ) -> Scalar
where D: Distribution, Scalar: UnsignedInteger + RandomGenerable<D, CustomModulus = Scalar>,

Generate a random unsigned integer from the given distribution under a custom modulus. This is only supported for unsigned integers for now.

§Example
use tfhe::core_crypto::commons::ciphertext_modulus::CiphertextModulus;
use tfhe::core_crypto::commons::math::random::{Gaussian, RandomGenerator, Uniform};
use tfhe_csprng::generators::SoftwareRandomGenerator;
use tfhe_csprng::seeders::Seed;

let mut generator = RandomGenerator::<SoftwareRandomGenerator>::new(Seed(0));

let custom_modulus = CiphertextModulus::new((1 << 64) - (1 << 32) + 1);

let random = generator.random_from_distribution_custom_mod::<u64, _>(Uniform, custom_modulus);
let random = generator.random_from_distribution_custom_mod::<u64, _>(
    Gaussian {
        mean: 0.0,
        std: 1.0,
    },
    custom_modulus,
);
Source

pub fn fill_slice_with_random_from_distribution<Scalar, D>( &mut self, output: &mut [Scalar], distribution: D, )
where D: Distribution, Scalar: RandomGenerable<D>,

Generate a random scalar from the given distribution under the native modulus.

§Example
use tfhe::core_crypto::commons::math::random::{Gaussian, RandomGenerator, Uniform};
use tfhe_csprng::generators::SoftwareRandomGenerator;
use tfhe_csprng::seeders::Seed;

let mut generator = RandomGenerator::<SoftwareRandomGenerator>::new(Seed(0));

let mut random = vec![0u8; 8];
generator.fill_slice_with_random_from_distribution(&mut random, Uniform);
let mut random = vec![0i8; 8];
generator.fill_slice_with_random_from_distribution(&mut random, Uniform);
let mut random = vec![0u64; 8];
generator.fill_slice_with_random_from_distribution(
    &mut random,
    Gaussian {
        mean: 0.0,
        std: 1.0,
    },
);
Source

pub fn fill_slice_with_random_from_distribution_custom_mod<Scalar, D>( &mut self, output: &mut [Scalar], distribution: D, custom_modulus: CiphertextModulus<Scalar>, )
where D: Distribution, Scalar: UnsignedInteger + RandomGenerable<D, CustomModulus = Scalar>,

Generate a random unsigned integer from the given distribution under a custom modulus. This is only supported for unsigned integers for now.

§Example
use tfhe::core_crypto::commons::ciphertext_modulus::CiphertextModulus;
use tfhe::core_crypto::commons::math::random::{Gaussian, RandomGenerator, Uniform};
use tfhe_csprng::generators::SoftwareRandomGenerator;
use tfhe_csprng::seeders::Seed;

let mut generator = RandomGenerator::<SoftwareRandomGenerator>::new(Seed(0));

let custom_modulus = CiphertextModulus::new((1 << 64) - (1 << 32) + 1);

let mut random = vec![0u64; 8];
generator.fill_slice_with_random_from_distribution_custom_mod(
    &mut random,
    Uniform,
    custom_modulus,
);
let mut random = vec![0u64; 8];
generator.fill_slice_with_random_from_distribution_custom_mod(
    &mut random,
    Gaussian {
        mean: 0.0,
        std: 1.0,
    },
    custom_modulus,
);
Source

pub fn unsigned_integer_slice_wrapping_add_random_from_distribution_assign<Scalar, D>( &mut self, output: &mut [Scalar], distribution: D, )

Add a random scalar from the given distribution under the native modulus.

§Example
use tfhe::core_crypto::commons::math::random::{Gaussian, RandomGenerator, Uniform};
use tfhe_csprng::generators::SoftwareRandomGenerator;
use tfhe_csprng::seeders::Seed;

let mut generator = RandomGenerator::<SoftwareRandomGenerator>::new(Seed(0));

let mut random = vec![0u8; 8];
generator
    .unsigned_integer_slice_wrapping_add_random_from_distribution_assign(&mut random, Uniform);
let mut random = vec![0u64; 8];
generator.unsigned_integer_slice_wrapping_add_random_from_distribution_assign(
    &mut random,
    Gaussian {
        mean: 0.0,
        std: 1.0,
    },
);
Source

pub fn unsigned_integer_slice_wrapping_add_random_from_distribution_custom_mod_assign<Scalar, D>( &mut self, output: &mut [Scalar], distribution: D, custom_modulus: CiphertextModulus<Scalar>, )
where D: Distribution, Scalar: UnsignedInteger + RandomGenerable<D, CustomModulus = Scalar>,

Add a random gaussian value to each element in a slice.

§Example
use tfhe::core_crypto::commons::ciphertext_modulus::CiphertextModulus;
use tfhe::core_crypto::commons::math::random::{Gaussian, RandomGenerator, Uniform};
use tfhe_csprng::generators::SoftwareRandomGenerator;
use tfhe_csprng::seeders::Seed;

let mut generator = RandomGenerator::<SoftwareRandomGenerator>::new(Seed(0));

let custom_mod_u8 = CiphertextModulus::try_new_power_of_2(7).unwrap();
let mut random = vec![0u8; 8];
generator.unsigned_integer_slice_wrapping_add_random_from_distribution_custom_mod_assign(
    &mut random,
    Uniform,
    custom_mod_u8,
);

let custom_mod_u64 = CiphertextModulus::try_new_power_of_2(63).unwrap();
let mut random = vec![0u64; 8];
generator.unsigned_integer_slice_wrapping_add_random_from_distribution_custom_mod_assign(
    &mut random,
    Gaussian {
        mean: 0.0,
        std: 1.0,
    },
    custom_mod_u64,
);
Source

pub fn random_uniform<Scalar>(&mut self) -> Scalar
where Scalar: RandomGenerable<Uniform>,

Generate a random uniform unsigned integer.

§Example
use tfhe::core_crypto::commons::math::random::RandomGenerator;
use tfhe_csprng::generators::SoftwareRandomGenerator;
use tfhe_csprng::seeders::Seed;
let mut generator = RandomGenerator::<SoftwareRandomGenerator>::new(Seed(0));

let random = generator.random_uniform::<u8>();
let random = generator.random_uniform::<u16>();
let random = generator.random_uniform::<u32>();
let random = generator.random_uniform::<u64>();
let random = generator.random_uniform::<u128>();

let random = generator.random_uniform::<i8>();
let random = generator.random_uniform::<i16>();
let random = generator.random_uniform::<i32>();
let random = generator.random_uniform::<i64>();
let random = generator.random_uniform::<i128>();
Source

pub fn random_uniform_custom_mod<Scalar>( &mut self, custom_modulus: CiphertextModulus<Scalar>, ) -> Scalar
where Scalar: UnsignedInteger + RandomGenerable<Uniform, CustomModulus = Scalar>,

Generate a random uniform unsigned integer. This is only supported for unsigned integers at the moment.

§Example
use tfhe::core_crypto::commons::ciphertext_modulus::CiphertextModulus;
use tfhe::core_crypto::commons::math::random::RandomGenerator;
use tfhe_csprng::generators::SoftwareRandomGenerator;
use tfhe_csprng::seeders::Seed;
let mut generator = RandomGenerator::<SoftwareRandomGenerator>::new(Seed(0));

let random =
    generator.random_uniform_custom_mod::<u8>(CiphertextModulus::try_new(1 << 8).unwrap());
let random =
    generator.random_uniform_custom_mod::<u16>(CiphertextModulus::try_new(1 << 8).unwrap());
let random =
    generator.random_uniform_custom_mod::<u32>(CiphertextModulus::try_new(1 << 8).unwrap());
let random =
    generator.random_uniform_custom_mod::<u64>(CiphertextModulus::try_new(1 << 8).unwrap());
let random =
    generator.random_uniform_custom_mod::<u128>(CiphertextModulus::try_new(1 << 8).unwrap());
Source

pub fn fill_slice_with_random_uniform<Scalar>(&mut self, output: &mut [Scalar])
where Scalar: RandomGenerable<Uniform>,

Fill a slice with random uniform values.

§Example
use tfhe::core_crypto::commons::math::random::RandomGenerator;
use tfhe_csprng::generators::SoftwareRandomGenerator;
use tfhe_csprng::seeders::Seed;
let mut generator = RandomGenerator::<SoftwareRandomGenerator>::new(Seed(0));
let mut vec = vec![0u32; 1000];
generator.fill_slice_with_random_uniform(&mut vec);
assert!(vec.iter().any(|&x| x != 0));
Source

pub fn fill_slice_with_random_uniform_custom_mod<Scalar>( &mut self, output: &mut [Scalar], custom_modulus: CiphertextModulus<Scalar>, )
where Scalar: UnsignedInteger + RandomGenerable<Uniform, CustomModulus = Scalar>,

Fill a slice with random uniform values.

§Example
use tfhe::core_crypto::commons::math::random::RandomGenerator;
use tfhe::core_crypto::commons::parameters::CiphertextModulus;
use tfhe_csprng::generators::SoftwareRandomGenerator;
use tfhe_csprng::seeders::Seed;
let mut generator = RandomGenerator::<SoftwareRandomGenerator>::new(Seed(0));
let mut vec = vec![0u32; 1000];
generator.fill_slice_with_random_uniform_custom_mod(
    &mut vec,
    CiphertextModulus::try_new_power_of_2(31).unwrap(),
);
assert!(vec.iter().any(|&x| x != 0));
Source

pub fn random_uniform_binary<Scalar>(&mut self) -> Scalar

Generate a random uniform binary value. This will draw one full byte from the underlying csprng.

§Example
use tfhe::core_crypto::commons::math::random::RandomGenerator;
use tfhe_csprng::generators::SoftwareRandomGenerator;
use tfhe_csprng::seeders::Seed;
let mut generator = RandomGenerator::<SoftwareRandomGenerator>::new(Seed(0));
let random: u32 = generator.random_uniform_binary();
Source

pub fn fill_slice_with_random_uniform_binary<Scalar>( &mut self, output: &mut [Scalar], )

Fill a slice with random uniform binary values. This will draw one full byte from the underlying csprng for every element of the slice.

§Example
use tfhe::core_crypto::commons::math::random::RandomGenerator;
use tfhe_csprng::generators::SoftwareRandomGenerator;
use tfhe_csprng::seeders::Seed;
let mut generator = RandomGenerator::<SoftwareRandomGenerator>::new(Seed(0));
let mut vec = vec![0u32; 1000];
generator.fill_slice_with_random_uniform_binary(&mut vec);
assert!(vec.iter().any(|&x| x != 0));
Source

pub fn fill_slice_with_random_uniform_binary_bits<Scalar>( &mut self, output: &mut [Scalar], )
where Scalar: UnsignedInteger,

Fill a slice with random uniform binary values. This will only draw as many bytes needed from the underlying csprng to fill the slice with random bits. If the slice len is n, it will draw ceil(n/8) bytes from the csprng.

§Example
use tfhe::core_crypto::commons::math::random::RandomGenerator;
use tfhe_csprng::generators::SoftwareRandomGenerator;
use tfhe_csprng::seeders::Seed;
let mut generator = RandomGenerator::<SoftwareRandomGenerator>::new(Seed(0));
let mut vec = vec![0u32; 1000];
generator.fill_slice_with_random_uniform_binary_bits(&mut vec);
assert!(vec.iter().any(|&x| x != 0));
Source

pub fn random_uniform_ternary<Scalar>(&mut self) -> Scalar

Generate a random uniform ternary value. This will draw one full byte from the underlying csprng for every element of the slice.

§Example
use tfhe::core_crypto::commons::math::random::RandomGenerator;
use tfhe_csprng::generators::SoftwareRandomGenerator;
use tfhe_csprng::seeders::Seed;
let mut generator = RandomGenerator::<SoftwareRandomGenerator>::new(Seed(0));
let random: u32 = generator.random_uniform_ternary();
Source

pub fn random_gaussian<Float, Scalar>( &mut self, mean: Float, std: Float, ) -> (Scalar, Scalar)

Generate two floating point values drawn from a gaussian distribution with input mean and standard deviation.

§Example
use tfhe::core_crypto::commons::math::random::RandomGenerator;
use tfhe_csprng::generators::SoftwareRandomGenerator;
use tfhe_csprng::seeders::Seed;
let mut generator = RandomGenerator::<SoftwareRandomGenerator>::new(Seed(0));
// for f32
let (g1, g2): (f32, f32) = generator.random_gaussian(0_f32, 1_f32);
// check that both samples are in 6 sigmas.
assert!(g1.abs() <= 6.);
assert!(g2.abs() <= 6.);
// for f64
let (g1, g2): (f64, f64) = generator.random_gaussian(0_f64, 1_f64);
// check that both samples are in 6 sigmas.
assert!(g1.abs() <= 6.);
assert!(g2.abs() <= 6.);
Source

pub fn fill_slice_with_random_gaussian<Float, Scalar>( &mut self, output: &mut [Scalar], mean: Float, std: Float, )

Fill a slice with random gaussian values.

§Example
use tfhe::core_crypto::commons::math::random::RandomGenerator;
use tfhe_csprng::generators::SoftwareRandomGenerator;
use tfhe_csprng::seeders::Seed;
let mut generator = RandomGenerator::<SoftwareRandomGenerator>::new(Seed(0));
let mut vec = vec![0f32; 1000];
generator.fill_slice_with_random_gaussian(&mut vec, 0., 1.);
assert!(vec.iter().any(|&x| x != 0.));
Source

pub fn fill_slice_with_random_gaussian_custom_mod<Float, Scalar>( &mut self, output: &mut [Scalar], mean: Float, std: Float, custom_modulus: CiphertextModulus<Scalar>, )
where Float: FloatingPoint, Scalar: UnsignedTorus + CastInto<Float>, (Scalar, Scalar): RandomGenerable<Gaussian<Float>, CustomModulus = Scalar>,

Fill a slice with random gaussian values.

§Example
use tfhe::core_crypto::commons::math::random::RandomGenerator;
use tfhe::core_crypto::commons::parameters::CiphertextModulus;
use tfhe_csprng::generators::SoftwareRandomGenerator;
use tfhe_csprng::seeders::Seed;
let mut generator = RandomGenerator::<SoftwareRandomGenerator>::new(Seed(0));
let mut vec = vec![0u64; 1000];
generator.fill_slice_with_random_gaussian_custom_mod(
    &mut vec,
    0.,
    1.,
    CiphertextModulus::try_new_power_of_2(63).unwrap(),
);
assert!(vec.iter().any(|&x| x != 0));
Source

pub fn unsigned_torus_slice_wrapping_add_random_gaussian_assign<Float, Scalar>( &mut self, output: &mut [Scalar], mean: Float, std: Float, )

Add a random gaussian value to each element in a slice.

§Example
use tfhe::core_crypto::commons::math::random::RandomGenerator;
use tfhe_csprng::generators::SoftwareRandomGenerator;
use tfhe_csprng::seeders::Seed;
let mut generator = RandomGenerator::<SoftwareRandomGenerator>::new(Seed(0));
let mut vec = vec![0u32; 1000];
generator.unsigned_torus_slice_wrapping_add_random_gaussian_assign(&mut vec, 0., 1.);
assert!(vec.iter().any(|&x| x != 0));
Source

pub fn unsigned_torus_slice_wrapping_add_random_gaussian_custom_mod_assign<Float, Scalar>( &mut self, output: &mut [Scalar], mean: Float, std: Float, custom_modulus: CiphertextModulus<Scalar>, )
where Float: FloatingPoint, Scalar: UnsignedTorus + CastInto<Float>, (Scalar, Scalar): RandomGenerable<Gaussian<Float>, CustomModulus = Scalar>,

Add a random gaussian value to each element in a slice.

§Example
use tfhe::core_crypto::commons::math::random::RandomGenerator;
use tfhe::core_crypto::commons::parameters::CiphertextModulus;
use tfhe_csprng::generators::SoftwareRandomGenerator;
use tfhe_csprng::seeders::Seed;
let mut generator = RandomGenerator::<SoftwareRandomGenerator>::new(Seed(0));
let mut vec = vec![0u32; 1000];
generator.unsigned_torus_slice_wrapping_add_random_gaussian_custom_mod_assign(
    &mut vec,
    0.,
    1.,
    CiphertextModulus::try_new_power_of_2(31).unwrap(),
);
assert!(vec.iter().any(|&x| x != 0));
Source§

impl<G: ParallelByteRandomGenerator> RandomGenerator<G>

Source

pub fn par_try_fork( &mut self, n_child: usize, bytes_per_child: usize, ) -> Result<impl IndexedParallelIterator<Item = Self>, ForkError>

Tries to fork the current generator into n_child generator bounded to bytes_per_child, as a parallel iterator.

If n_child*bytes_per_child exceeds the bound of the current generator, the method returns None.

§Example
use tfhe::core_crypto::commons::math::random::RandomGenerator;
use tfhe_csprng::generators::SoftwareRandomGenerator;
use tfhe_csprng::seeders::Seed;
let mut generator = RandomGenerator::<SoftwareRandomGenerator>::new(Seed(0));
let children = generator.try_fork(5, 50).unwrap().collect::<Vec<_>>();

Trait Implementations§

Source§

impl<G: ByteRandomGenerator> RngCore for RandomGenerator<G>

Source§

fn next_u32(&mut self) -> u32

Return the next random u32. Read more
Source§

fn next_u64(&mut self) -> u64

Return the next random u64. Read more
Source§

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

Fill dest with random data. Read more
Source§

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>

Fill dest entirely with random data. Read more

Auto Trait Implementations§

§

impl<G> Freeze for RandomGenerator<G>
where G: Freeze,

§

impl<G> RefUnwindSafe for RandomGenerator<G>
where G: RefUnwindSafe,

§

impl<G> Send for RandomGenerator<G>
where G: Send,

§

impl<G> Sync for RandomGenerator<G>
where G: Sync,

§

impl<G> Unpin for RandomGenerator<G>
where G: Unpin,

§

impl<G> UnsafeUnpin for RandomGenerator<G>
where G: UnsafeUnpin,

§

impl<G> UnwindSafe for RandomGenerator<G>
where G: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Input, Output> CastInto<Output> for Input
where Output: CastFrom<Input>,

Source§

fn cast_into(self) -> Output

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<R> Rng for R
where R: RngCore + ?Sized,

Source§

fn gen<T>(&mut self) -> T

Return a random value supporting the Standard distribution. Read more
Source§

fn gen_range<T, R>(&mut self, range: R) -> T
where T: SampleUniform, R: SampleRange<T>,

Generate a random value in the given range. Read more
Source§

fn sample<T, D>(&mut self, distr: D) -> T
where D: Distribution<T>,

Sample a new value, using the given distribution. Read more
Source§

fn sample_iter<T, D>(self, distr: D) -> DistIter<D, Self, T>
where D: Distribution<T>, Self: Sized,

Create an iterator that generates values using the given distribution. Read more
Source§

fn fill<T>(&mut self, dest: &mut T)
where T: Fill + ?Sized,

Fill any type implementing Fill with random data Read more
Source§

fn try_fill<T>(&mut self, dest: &mut T) -> Result<(), Error>
where T: Fill + ?Sized,

Fill any type implementing Fill with random data Read more
Source§

fn gen_bool(&mut self, p: f64) -> bool

Return a bool with a probability p of being true. Read more
Source§

fn gen_ratio(&mut self, numerator: u32, denominator: u32) -> bool

Return a bool with a probability of numerator/denominator of being true. I.e. gen_ratio(2, 3) has chance of 2 in 3, or about 67%, of returning true. If numerator == denominator, then the returned value is guaranteed to be true. If numerator == 0, then the returned value is guaranteed to be false. Read more
Source§

impl<A> SafeAs for A

Source§

fn sas<T>(self) -> T
where A: TryInto<T>,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more