Add a RandomBits distribution
This commit is contained in:
parent
3a3cb2d574
commit
e679836303
@ -1,6 +1,6 @@
|
||||
//! Randomization of big integers
|
||||
|
||||
use rand::Rng;
|
||||
use rand::prelude::*;
|
||||
use rand::distributions::uniform::{SampleUniform, UniformSampler};
|
||||
|
||||
use BigInt;
|
||||
@ -180,3 +180,30 @@ impl UniformSampler for UniformBigInt {
|
||||
impl SampleUniform for BigInt {
|
||||
type Sampler = UniformBigInt;
|
||||
}
|
||||
|
||||
/// A random distribution for `BigUint` and `BigInt` values of a particular bit size.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct RandomBits {
|
||||
bits: usize,
|
||||
}
|
||||
|
||||
impl RandomBits {
|
||||
#[inline]
|
||||
pub fn new(bits: usize) -> RandomBits {
|
||||
RandomBits { bits }
|
||||
}
|
||||
}
|
||||
|
||||
impl Distribution<BigUint> for RandomBits {
|
||||
#[inline]
|
||||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> BigUint {
|
||||
rng.gen_biguint(self.bits)
|
||||
}
|
||||
}
|
||||
|
||||
impl Distribution<BigInt> for RandomBits {
|
||||
#[inline]
|
||||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> BigInt {
|
||||
rng.gen_bigint(self.bits)
|
||||
}
|
||||
}
|
||||
|
@ -164,7 +164,7 @@ pub use bigint::BigInt;
|
||||
pub use bigint::ToBigInt;
|
||||
|
||||
#[cfg(feature = "rand")]
|
||||
pub use bigrand::{RandBigInt, UniformBigUint, UniformBigInt};
|
||||
pub use bigrand::{RandBigInt, RandomBits, UniformBigUint, UniformBigInt};
|
||||
|
||||
mod big_digit {
|
||||
/// A `BigDigit` is a `BigUint`'s composing element.
|
||||
|
@ -5,7 +5,7 @@ extern crate num_traits;
|
||||
extern crate rand;
|
||||
|
||||
mod biguint {
|
||||
use num_bigint::{BigUint, RandBigInt};
|
||||
use num_bigint::{BigUint, RandBigInt, RandomBits};
|
||||
use num_traits::Zero;
|
||||
use rand::thread_rng;
|
||||
use rand::Rng;
|
||||
@ -14,10 +14,20 @@ mod biguint {
|
||||
#[test]
|
||||
fn test_rand() {
|
||||
let mut rng = thread_rng();
|
||||
let _n: BigUint = rng.gen_biguint(137);
|
||||
let n: BigUint = rng.gen_biguint(137);
|
||||
assert!(n.bits() <= 137);
|
||||
assert!(rng.gen_biguint(0).is_zero());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rand_bits() {
|
||||
let mut rng = thread_rng();
|
||||
let n: BigUint = rng.sample(&RandomBits::new(137));
|
||||
assert!(n.bits() <= 137);
|
||||
let z: BigUint = rng.sample(&RandomBits::new(0));
|
||||
assert!(z.is_zero());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rand_range() {
|
||||
let mut rng = thread_rng();
|
||||
@ -82,7 +92,7 @@ mod biguint {
|
||||
}
|
||||
|
||||
mod bigint {
|
||||
use num_bigint::{BigInt, RandBigInt};
|
||||
use num_bigint::{BigInt, RandBigInt, RandomBits};
|
||||
use num_traits::Zero;
|
||||
use rand::thread_rng;
|
||||
use rand::Rng;
|
||||
@ -91,10 +101,20 @@ mod bigint {
|
||||
#[test]
|
||||
fn test_rand() {
|
||||
let mut rng = thread_rng();
|
||||
let _n: BigInt = rng.gen_bigint(137);
|
||||
let n: BigInt = rng.gen_bigint(137);
|
||||
assert!(n.bits() <= 137);
|
||||
assert!(rng.gen_bigint(0).is_zero());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rand_bits() {
|
||||
let mut rng = thread_rng();
|
||||
let n: BigInt = rng.sample(&RandomBits::new(137));
|
||||
assert!(n.bits() <= 137);
|
||||
let z: BigInt = rng.sample(&RandomBits::new(0));
|
||||
assert!(z.is_zero());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rand_range() {
|
||||
let mut rng = thread_rng();
|
||||
|
Loading…
x
Reference in New Issue
Block a user