junobuild_shared/
random.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use candid::Principal;
use ic_cdk::api::call::CallResult;
use ic_cdk::call;
use rand::{rngs::StdRng, SeedableRng};

pub async fn get_random_seed() -> Option<StdRng> {
    let result: CallResult<([u8; 32],)> =
        call(Principal::management_canister(), "raw_rand", ()).await;

    match result {
        // We do nothing in case of error to not block initialization but, getrandom will throw errors
        Err(_) => None,
        Ok((seed,)) => Some(StdRng::from_seed(seed)),
    }
}