Expand description
Random integers generation with the rand
crate.
There are four distributions for generating random integers. The first two are UniformBits, and UniformBelow which limit the bit size or the magnitude of the generated integer. The other two are UniformUBig and UniformIBig, which supports generating random integers uniformly in a given range. These traits are also the backends for the SampleUniform trait.
§Examples
use dashu_base::BitTest;
use dashu_int::rand::{UniformBits, UniformBelow};
// generate UBigs in a range
let a = thread_rng().gen_range(UBig::from(3u8)..UBig::from(10u8));
let b = thread_rng().sample(Uniform::new(UBig::ZERO, &a));
assert!(a >= UBig::from(3u8) && a < UBig::from(10u8));
assert!(b >= UBig::ZERO && b < a);
// generate IBigs in a range
let a = thread_rng().gen_range(IBig::from(3)..IBig::from(10));
let b = thread_rng().sample(Uniform::new(IBig::from(-5), &a));
assert!(a >= IBig::from(3) && a < IBig::from(10));
assert!(b >= IBig::from(-5) && b < a);
// generate UBigs and IBigs with a given bit size limit.
let a: UBig = thread_rng().sample(UniformBits::new(10));
let b: IBig = thread_rng().sample(UniformBits::new(10));
assert!(a.bit_len() <= 10 && b.bit_len() <= 10);
// generate UBigs and IBigs with a given magnitude limit.
let a: UBig = thread_rng().sample(UniformBelow::new(&10u8.into()));
let b: IBig = thread_rng().sample(UniformBelow::new(&10u8.into()));
assert!(a < UBig::from(10u8));
assert!(b < IBig::from(10) && b > IBig::from(-10));
Structs§
- Uniform
Below - Uniform distribution around zero for both UBig and IBig specified by a limit of the magnitude.
- Uniform
Bits - Uniform distribution for both UBig and IBig specified by bits.
- UniformI
Big - The back-end implementing UniformSampler for IBig.
- UniformU
Big - The back-end implementing UniformSampler for UBig.