I need a random 256-bit unsigned number. I discovered that there is the RandBigInt
trait with the method gen_biguint()
, but I am having a tough time finding an implementation for it.
I tried doing this some time ago for BigInt
. The crates have been updated since. This is how I am using it to get a BigUint
now.
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;
}
_ => {}
}
}
}
Contents from my Cargo.toml
num = "0.1.42"
rand = "0.4.2"
I am sure there must be some straight-forward way of achieving this.