Struct soroban_sdk::prng::Prng
source · pub struct Prng { /* private fields */ }
Expand description
Prng is a pseudo-random generator.
Warning
The PRNG is unsuitable for generating secrets or use in applications with low risk tolerance, see the module-level comment.
Implementations§
source§impl Prng
impl Prng
pub fn env(&self) -> &Env
sourcepub fn seed(&self, seed: Bytes)
pub fn seed(&self, seed: Bytes)
Reseeds the PRNG with the provided value.
Warning
The PRNG is unsuitable for generating secrets or use in applications with low risk tolerance, see the module-level comment.
sourcepub fn fill<T>(&self, v: &mut T)
pub fn fill<T>(&self, v: &mut T)
Fills the type with a random value.
Warning
The PRNG is unsuitable for generating secrets or use in applications with low risk tolerance, see the module-level comment.
Examples
u64
let mut value: u64 = 0;
env.prng().fill(&mut value);
assert_eq!(value, 8478755077819529274);
[u8]
let mut value = [0u8; 32];
env.prng().fill(&mut value);
assert_eq!(
value,
[
58, 248, 248, 38, 210, 150, 170, 117, 122, 110, 9, 101, 244, 57,
221, 102, 164, 48, 43, 104, 222, 229, 242, 29, 25, 148, 88, 204,
130, 148, 2, 66
],
);
sourcepub fn gen<T>(&self) -> Twhere
T: Gen,
pub fn gen<T>(&self) -> Twhere
T: Gen,
Returns a random value of the given type.
Warning
The PRNG is unsuitable for generating secrets or use in applications with low risk tolerance, see the module-level comment.
Examples
u64
let value: u64 = env.prng().gen();
assert_eq!(value, 8478755077819529274);
[u8; N]
let value: [u8; 32] = env.prng().gen();
assert_eq!(
value,
[
58, 248, 248, 38, 210, 150, 170, 117, 122, 110, 9, 101, 244, 57,
221, 102, 164, 48, 43, 104, 222, 229, 242, 29, 25, 148, 88, 204,
130, 148, 2, 66
],
);
sourcepub fn gen_len<T>(&self, len: T::Len) -> Twhere
T: GenLen,
pub fn gen_len<T>(&self, len: T::Len) -> Twhere
T: GenLen,
Returns a random value of the given type with the given length.
Panics
If the length is greater than u32::MAX.
Warning
The PRNG is unsuitable for generating secrets or use in applications with low risk tolerance, see the module-level comment.
Examples
Bytes
// Get a value of length 32 bytes.
let value: Bytes = env.prng().gen_len(32);
assert_eq!(value, Bytes::from_slice(
&env,
&[
58, 248, 248, 38, 210, 150, 170, 117, 122, 110, 9, 101, 244, 57,
221, 102, 164, 48, 43, 104, 222, 229, 242, 29, 25, 148, 88, 204,
130, 148, 2, 66
],
));
sourcepub fn gen_range<T>(&self, r: impl RangeBounds<T::RangeBound>) -> Twhere
T: GenRange,
pub fn gen_range<T>(&self, r: impl RangeBounds<T::RangeBound>) -> Twhere
T: GenRange,
Returns a random value of the given type in the range specified.
Panics
If the start of the range is greater than the end.
Warning
The PRNG is unsuitable for generating secrets or use in applications with low risk tolerance, see the module-level comment.
Examples
u64
// Get a value in the range of 1 to 100, inclusive.
let value: u64 = env.prng().gen_range(1..=100);
assert_eq!(value, 46);
sourcepub fn u64_in_range(&self, r: impl RangeBounds<u64>) -> u64
👎Deprecated: use env.prng().gen_range(…)
pub fn u64_in_range(&self, r: impl RangeBounds<u64>) -> u64
Returns a random u64 in the range specified.
Panics
If the range is empty.
Warning
The PRNG is unsuitable for generating secrets or use in applications with low risk tolerance, see the module-level comment.
Examples
// Get a value in the range of 1 to 100, inclusive.
let value = env.prng().u64_in_range(1..=100);
assert_eq!(value, 46);
Auto Trait Implementations§
impl !RefUnwindSafe for Prng
impl !Send for Prng
impl !Sync for Prng
impl Unpin for Prng
impl !UnwindSafe for Prng
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
§impl<T, U, V, E, C> Compare<(T, U, V)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E> + Compare<U> + Compare<V>,
impl<T, U, V, E, C> Compare<(T, U, V)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E> + Compare<U> + Compare<V>,
§impl<T, U, V, W, E, C> Compare<(T, U, V, W)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E, Error = E> + Compare<U> + Compare<V> + Compare<W>,
impl<T, U, V, W, E, C> Compare<(T, U, V, W)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E, Error = E> + Compare<U> + Compare<V> + Compare<W>,
type Error = E
fn compare( &self, a: &(T, U, V, W), b: &(T, U, V, W) ) -> Result<Ordering, <C as Compare<(T, U, V, W)>>::Error>
§impl<T, U, V, W, X, E, C> Compare<(T, U, V, W, X)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E, Error = E, Error = E> + Compare<U> + Compare<V> + Compare<W> + Compare<X>,
impl<T, U, V, W, X, E, C> Compare<(T, U, V, W, X)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E, Error = E, Error = E> + Compare<U> + Compare<V> + Compare<W> + Compare<X>,
type Error = E
fn compare( &self, a: &(T, U, V, W, X), b: &(T, U, V, W, X) ) -> Result<Ordering, <C as Compare<(T, U, V, W, X)>>::Error>
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.