Random bigint error

I try to use rand and num::bigint to generate random big unsigned integer,

here's roughly how I do it:

use num::{bigint::RandBigInt, BigUint};
use rand;

let mut rng = rand::thread_rng();
let r = RandBigInt::gen_biguint_below(&mut rng, &self.p); // self.p is a `BigUint` prime number

and in my Cargo.toml:

[dependencies]
num = { version = "0.2.1", features = ["rand"] }
rand = "0.7"

And here's the error I got:

error[E0277]: the trait bound `rand::rngs::thread::ThreadRng: rand_core::RngCore` is not satisfied
   --> dh/src/mod_p.rs:51:47
    |
51  |         let r = RandBigInt::gen_biguint_below(&mut rng, &self.p);
    |                                               ^^^^^^^^ the trait `rand_core::RngCore` is not implemented for `rand::rngs::thread::ThreadRng`
    |
help: trait impl with same name found
   --> /Users/alex/.cargo/registry/src/github.com-1ecc6299db9ec823/rand_core-0.5.1/src/lib.rs:388:1
    |
388 | / impl<'a, R: RngCore + ?Sized> RngCore for &'a mut R {
389 | |     #[inline(always)]
390 | |     fn next_u32(&mut self) -> u32 {
391 | |         (**self).next_u32()
...   |
407 | |     }
408 | | }
    | |_^
    = note: perhaps two different versions of crate `rand_core` are being used?
    = note: required because of the requirements on the impl of `rand::Rng` for `rand::rngs::thread::ThreadRng`
    = note: required because of the requirements on the impl of `num_bigint::bigrand::RandBigInt` for `rand::rngs::thread::ThreadRng`
    = note: required by `num_bigint::bigrand::RandBigInt::gen_biguint_below`

Anyone has any idea why?

At this time, the published num crates are only compatible with rand 0.5. The git master branch is preparing num 0.3 using rand 0.7, if you'd like to try that out...

1 Like

Thanks man!

num 0.3 is not out yet, is it?

Also in situation like this, is it more canonical to bump up my num version or downgrade my rand version?

It's not out yet. The version in git is actually 0.3.0-pre right now, and you won't find it on crates.io.

If you want to publish your code that's using this, then you'll need to use the one on crates.io, version 0.2.1, and lower rand to match. But if you're willing to test the newer version, you can use a git path for your dependency -- and please report any issues!

1 Like

@cuviper roger, and thanks again!