Struct tp_runtime::random_number_generator::RandomNumberGenerator [−][src]
pub struct RandomNumberGenerator<Hashing: Hash> { /* fields omitted */ }
Expand description
Pseudo-random number streamer. This retains the state of the random number stream. It’s as secure as the combination of the seed with which it is constructed and the hash function it uses to cycle elements.
It can be saved and later reloaded using the Codec traits.
Example:
use tp_runtime::traits::{Hash, BlakeTwo256}; use tp_runtime::RandomNumberGenerator; let random_seed = BlakeTwo256::hash(b"Sixty-nine"); let mut rng = <RandomNumberGenerator<BlakeTwo256>>::new(random_seed); assert_eq!(rng.pick_u32(100), 59); assert_eq!(rng.pick_item(&[1, 2, 3]), Some(&1));
This can use any cryptographic Hash
function as the means of entropy-extension, and avoids
needless extensions of entropy.
If you’re persisting it over blocks, be aware that the sequence will start to repeat. This won’t be a practical issue unless you’re using tiny hash types (e.g. 64-bit) and pulling hundred of megabytes of data from it.
Implementations
Returns a number at least zero, at most max
.
This returns a usize
, but internally it only uses u32
so avoid consensus problems.
Trait Implementations
impl<Hashing: Hash> EncodeLike<RandomNumberGenerator<Hashing>> for RandomNumberGenerator<Hashing> where
Hashing::Output: Encode,
Hashing::Output: Encode,
Auto Trait Implementations
impl<Hashing> RefUnwindSafe for RandomNumberGenerator<Hashing> where
<Hashing as Hash>::Output: RefUnwindSafe,
impl<Hashing> Send for RandomNumberGenerator<Hashing>
impl<Hashing> Sync for RandomNumberGenerator<Hashing>
impl<Hashing> Unpin for RandomNumberGenerator<Hashing> where
<Hashing as Hash>::Output: Unpin,
impl<Hashing> UnwindSafe for RandomNumberGenerator<Hashing> where
<Hashing as Hash>::Output: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more
impl<T> Downcast for T where
T: Any,
impl<T> Downcast for T where
T: Any,
pub fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>ⓘNotable traits for Box<R, Global>
impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
pub fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>ⓘNotable traits for Box<R, Global>
impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
Convert Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
. Read more
pub fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
pub fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
Convert Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
. Read more
Convert &Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s. Read more
pub fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
pub fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert &mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s. Read more
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
type Output = T
type Output = T
Should always be Self
The counterpart to unchecked_from
.
Consume self to return an equivalent value of T
.
pub fn vzip(self) -> V
impl<T> MaybeRefUnwindSafe for T where
T: RefUnwindSafe,