pub struct RandomNumberGenerator<Hashing: Hash> { /* private fields */ }
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.
(It is recommended to use the rand_chacha
crate as an alternative to this where possible.)
Example:
use sp_runtime::traits::{Hash, BlakeTwo256};
use ep_core::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§
source§impl<Hashing: Hash> RandomNumberGenerator<Hashing>
impl<Hashing: Hash> RandomNumberGenerator<Hashing>
sourcepub fn pick_non_zero_u32(&mut self, max: u32) -> u32
pub fn pick_non_zero_u32(&mut self, max: u32) -> u32
Returns a number at least 1, at most max
.
sourcepub fn pick_usize(&mut self, max: usize) -> usize
pub fn pick_usize(&mut self, max: usize) -> usize
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§
source§impl<Hashing: Hash> Decode for RandomNumberGenerator<Hashing>
impl<Hashing: Hash> Decode for RandomNumberGenerator<Hashing>
source§fn decode<__CodecInputEdqy: Input>(
__codec_input_edqy: &mut __CodecInputEdqy
) -> Result<Self, Error>
fn decode<__CodecInputEdqy: Input>( __codec_input_edqy: &mut __CodecInputEdqy ) -> Result<Self, Error>
source§fn decode_into<I>(
input: &mut I,
dst: &mut MaybeUninit<Self>
) -> Result<DecodeFinished, Error>where
I: Input,
fn decode_into<I>(
input: &mut I,
dst: &mut MaybeUninit<Self>
) -> Result<DecodeFinished, Error>where
I: Input,
source§impl<Hashing: Hash> Encode for RandomNumberGenerator<Hashing>
impl<Hashing: Hash> Encode for RandomNumberGenerator<Hashing>
source§fn size_hint(&self) -> usize
fn size_hint(&self) -> usize
source§fn encode_to<__CodecOutputEdqy: Output + ?Sized>(
&self,
__codec_dest_edqy: &mut __CodecOutputEdqy
)
fn encode_to<__CodecOutputEdqy: Output + ?Sized>( &self, __codec_dest_edqy: &mut __CodecOutputEdqy )
source§fn using_encoded<R, F>(&self, f: F) -> R
fn using_encoded<R, F>(&self, f: F) -> R
source§fn encoded_size(&self) -> usize
fn encoded_size(&self) -> usize
source§impl<Hashing> TypeInfo for RandomNumberGenerator<Hashing>
impl<Hashing> TypeInfo for RandomNumberGenerator<Hashing>
impl<Hashing: Hash> EncodeLike for RandomNumberGenerator<Hashing>
Auto Trait Implementations§
impl<Hashing> RefUnwindSafe for RandomNumberGenerator<Hashing>
impl<Hashing> Send for RandomNumberGenerator<Hashing>
impl<Hashing> Sync for RandomNumberGenerator<Hashing>
impl<Hashing> Unpin for RandomNumberGenerator<Hashing>
impl<Hashing> UnwindSafe for RandomNumberGenerator<Hashing>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CheckedConversion for T
impl<T> CheckedConversion for T
source§impl<T> DecodeLimit for Twhere
T: Decode,
impl<T> DecodeLimit for Twhere
T: Decode,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T, Outer> IsWrappedBy<Outer> for T
impl<T, Outer> IsWrappedBy<Outer> for T
source§impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
source§fn lossy_into(self) -> Dst
fn lossy_into(self) -> Dst
source§impl<T> SaturatedConversion for T
impl<T> SaturatedConversion for T
source§fn saturated_from<T>(t: T) -> Selfwhere
Self: UniqueSaturatedFrom<T>,
fn saturated_from<T>(t: T) -> Selfwhere
Self: UniqueSaturatedFrom<T>,
source§fn saturated_into<T>(self) -> Twhere
Self: UniqueSaturatedInto<T>,
fn saturated_into<T>(self) -> Twhere
Self: UniqueSaturatedInto<T>,
T
. Read moresource§impl<S, T> UncheckedInto<T> for Swhere
T: UncheckedFrom<S>,
impl<S, T> UncheckedInto<T> for Swhere
T: UncheckedFrom<S>,
source§fn unchecked_into(self) -> T
fn unchecked_into(self) -> T
unchecked_from
.source§impl<T, S> UniqueSaturatedInto<T> for S
impl<T, S> UniqueSaturatedInto<T> for S
source§fn unique_saturated_into(self) -> T
fn unique_saturated_into(self) -> T
T
.