diff --git a/src/header/stdlib/mod.rs b/src/header/stdlib/mod.rs index c69caa447ab40094b222ea88225d22560c8e1c04..c3b20ab4a40e4e77fb2aeaeb448f3f814b1595b5 100644 --- a/src/header/stdlib/mod.rs +++ b/src/header/stdlib/mod.rs @@ -1,7 +1,7 @@ //! stdlib implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/stdlib.h.html use core::{intrinsics, iter, mem, ptr, slice}; -use rand::distributions::Alphanumeric; +use rand::distributions::{Alphanumeric, Distribution, Uniform}; use rand::prng::XorShiftRng; use rand::rngs::JitterRng; use rand::{Rng, SeedableRng}; @@ -34,6 +34,10 @@ pub const MB_LEN_MAX: c_int = 4; static mut ATEXIT_FUNCS: [Option<extern "C" fn()>; 32] = [None; 32]; static mut RNG: Option<XorShiftRng> = None; +lazy_static! { + static ref RNG_SAMPLER: Uniform<c_int> = Uniform::new_inclusive(0, RAND_MAX); +} + #[no_mangle] pub extern "C" fn _Exit(status: c_int) { unistd::_exit(status); @@ -618,10 +622,10 @@ pub extern "C" fn qsort( #[no_mangle] pub unsafe extern "C" fn rand() -> c_int { match RNG { - Some(ref mut rng) => rng.gen_range(0, RAND_MAX), + Some(ref mut rng) => RNG_SAMPLER.sample(rng), None => { let mut rng = XorShiftRng::from_seed([1; 16]); - let ret = rng.gen_range(0, RAND_MAX); + let ret = RNG_SAMPLER.sample(&mut rng); RNG = Some(rng); ret } @@ -637,7 +641,7 @@ pub unsafe extern "C" fn rand_r(seed: *mut c_uint) -> c_int { let seed_arr: [u8; 16] = mem::transmute([*seed; 16 / mem::size_of::<c_uint>()]); let mut rng = XorShiftRng::from_seed(seed_arr); - let ret = rng.gen_range(0, RAND_MAX); + let ret = RNG_SAMPLER.sample(&mut rng); *seed = ret as _; diff --git a/tests/expected/stdlib/rand.stdout b/tests/expected/stdlib/rand.stdout index 4e35003bf8c2569ba9b9223c9129efd63ffb239c..2a50461db263d8c567c694d1c0425820204f25cb 100644 --- a/tests/expected/stdlib/rand.stdout +++ b/tests/expected/stdlib/rand.stdout @@ -3,6 +3,6 @@ 201425341 67141780 264204 -271585843 +271585844 264204 -271585843 +271585844