How to generate a random BigUint within a range of large numbers


I’m attempting to generate a BigUint value sourced from rand::OsRng. Preferably, I’d like for this random number to fall within a range of large numbers (such as the range of all 1024-bit numbers). I noticed that OsRng has a method called gen_range() which will do exactly that, however that function doesn’t support generating BigUints (well, more precisely, BigUint doesn’t implement the SampleRange trait.

Does anyone know of another way to generate BigUints? Or, if implementing SampleRange would be the smartest option, where the source code is for the implementation for SampleRange is for the primitive numeric types?

Thank you!


Use num::bigint::RandBigInt, which is implemented for all Rng types.


Awesome! Thanks for pointing that out. I suppose that I should have investigated a bit more before asking!