Function unc_sdk::env::random_seed_array

source ยท
pub fn random_seed_array() -> [u8; 32]
Expand description

Returns the random seed from the current block. This 32 byte hash is based on the VRF value from the block. This value is not modified in any way each time this function is called within the same method/block. Example of usage:

use rand::{Rng, SeedableRng};
use rand_chacha::ChaCha20Rng;
use unc_sdk::unc;
use unc_sdk::env;
#[unc(contract_state)]
struct RngExample {
   val: i32,
}
#[unc]
impl RngExample {
    pub fn increment(&mut self) {
        let mut rng = ChaCha20Rng::from_seed(env::random_seed_array());
        let value = rng.gen_range(0..1011);
        self.val += value;
    }
    pub fn get_value(&mut self) -> i32 {
        self.val
    }
}

Example of usage with unc-rng which allows to decrease size of contract binary:

use unc_rng::Rng;
use unc_sdk::unc;
use unc_sdk::env;
#[unc(contract_state)]
struct UncRngExample {
   val: i32,
}
#[unc]
impl UncRngExample {
    pub fn increment(&mut self) {
        let mut rng = Rng::new(&env::random_seed());
        let value = rng.rand_range_i32(0, 20);
        self.val += value;
    }
    pub fn get_value(&mut self) -> i32 {
        self.val
    }
}

More info in documentation