Aatch / ramp

RAMP - Rust Arithmetic in Multiple Precision
Apache License 2.0
261 stars 38 forks source link

[BUG] `ThreadRng::gen_uint_below()` hangs for large numbers #58

Closed rozbb closed 8 years ago

rozbb commented 8 years ago

Whenever I try to generate a number less than a certain large prime, the program seems to get caught in a loop. It takes up a bunch of CPU time and I've never waited long enough to see it (possibly) return a value. Here's a small program that will hang:

extern crate ramp;
extern crate rand;

use rand::Rng;
use ramp::int::{Int, RandomInt};

static P_521: &'static str = "\
    000001ffffffffffffffffffffffffffffffffffffffffff\
    ffffffffffffffffffffffffffffffffffffffffffffffff\
    ffffffffffffffffffffffffffffffffffffffff";

fn main() {
    let big = Int::from_str_radix(P_521, 16).unwrap();
    let small = Int::from(957);

    let mut rng = rand::thread_rng();

    let small_rand = rng.gen_uint_below(&small);
    println!("Small random {}", &small_rand);

    let big_rand = rng.gen_uint_below(&big); // Hangs here
    println!("Big random {}", &big_rand);
}