尝试创建随机数以const

问题描述 投票:0回答:1

我正在尝试使用

const_random
创建一个随机数为 const 值并获得相同的值

我有以下代码:

use const_random::const_random;


pub mod random_id{
    const NUMBER: u64 = const_random!(u64)
     
    pub fn get_number() - > u64 {
        return NUMBER;
    }
}

我总是得到相同的号码,我该如何解决它?

rust rust-cargo
1个回答
0
投票

这是您正在寻找的东西吗?

use lazy_static::lazy_static; // 1.4.0
use rand; // 0.8.5


lazy_static!{
    // On the first call, this produces a random value.
    // Subsequent calls will reuse the same value.
    // Value is refreshed upon each start of the program.
    static ref RANDOM : u64 = rand::random();
}

fn main() {
    println!("RANDOM: {}", *RANDOM)
}
© www.soinside.com 2019 - 2024. All rights reserved.