junobuild_shared/
random.rs

1use candid::Principal;
2use ic_cdk::api::call::CallResult;
3use ic_cdk::call;
4use rand::{rngs::StdRng, SeedableRng};
5
6pub async fn get_random_seed() -> Option<StdRng> {
7    let result: CallResult<([u8; 32],)> =
8        call(Principal::management_canister(), "raw_rand", ()).await;
9
10    match result {
11        // We do nothing in case of error to not block initialization but, getrandom will throw errors
12        Err(_) => None,
13        Ok((seed,)) => Some(StdRng::from_seed(seed)),
14    }
15}