【发布时间】:2018-08-01 18:49:20
【问题描述】:
我需要一个随机的 256 位无符号数。我发现RandBigInt 方法有RandBigInt 特征,但我很难找到它的实现。
我前段时间尝试为BigInt 执行此操作。从那以后,板条箱已经更新。这就是我现在使用它获取BigUint 的方式。
extern crate num;
extern crate rand;
use num::bigint::{BigInt, BigUint, RandBigInt};
fn main() {
let mut rng = rand::thread_rng();
let mut n: BigUint = BigUint::default();
loop {
let bigUint: Option<BigUint> = rng.gen_bigint(256).to_biguint();
match bigUint {
Some(num) => {
n = num.clone();
println!("Found the 1st BigUint - {}", num);
break;
}
_ => {}
}
}
}
我的 Cargo.toml
中的内容num = "0.1.42"
rand = "0.4.2"
我确信必须有一些直接的方法来实现这一点。
【问题讨论】: