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 near_sdk::near;
use near_sdk::env;
#[near(contract_state)]
struct RngExample {
   val: i32,
}
#[near]
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 near-rng which allows to decrease size of contract binary:

use near_rng::Rng;
use near_sdk::near;
use near_sdk::env;
#[near(contract_state)]
struct NearRngExample {
   val: i32,
}
#[near]
impl NearRngExample {
    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