lotus_script/
rand.rs

1//! Random number generation.
2use std::ops::{Bound, RangeBounds};
3
4/// Seed the random number generator with a random seed.
5pub fn random_seed() {
6    unsafe { lotus_script_sys::rand::random_seed() }
7}
8
9/// Seed the random number generator.
10pub fn seed(seed: u64) {
11    unsafe { lotus_script_sys::rand::seed(seed) }
12}
13
14/// Generate a random f64 in the range 0 to 1.
15pub fn gen_f64() -> f64 {
16    unsafe { lotus_script_sys::rand::f64() }
17}
18
19/// Generate a random u64 for the given range.
20pub fn gen_u64(range: impl RangeBounds<u64>) -> u64 {
21    let min = match range.start_bound() {
22        Bound::Included(min) => *min,
23        Bound::Excluded(min) => min + 1,
24        Bound::Unbounded => 0,
25    };
26
27    let max = match range.end_bound() {
28        Bound::Included(max) => *max,
29        Bound::Excluded(max) => max - 1,
30        Bound::Unbounded => u64::MAX,
31    };
32
33    assert!(min <= max, "min must be less than or equal to max");
34
35    unsafe { lotus_script_sys::rand::u64(min, max) }
36}