@@ -6,7 +6,6 @@ module rand
66
77import math.bits
88import rand.config
9- import rand.constants
109import rand.wyrand
1110import time
1211
@@ -165,13 +164,13 @@ pub fn (mut rng PRNG) i64() i64 {
165164// int31 returns a positive pseudorandom 31-bit `int`.
166165@[inline]
167166pub fn (mut rng PRNG) int31 () int {
168- return int (rng.u32 () & constants.u 31_ mask ) // Set the 32nd bit to 0.
167+ return int (rng.u32 () & u32 ( 0x7FFFFFFF ) ) // Set the 32nd bit to 0.
169168}
170169
171170// int63 returns a positive pseudorandom 63-bit `i64`.
172171@[inline]
173172pub fn (mut rng PRNG) int63 () i64 {
174- return i64 (rng.u64 () & constants.u 63_ mask ) // Set the 64th bit to 0.
173+ return i64 (rng.u64 () & u64 ( 0x7FFFFFFFFFFFFFFF ) ) // Set the 64th bit to 0.
175174}
176175
177176// intn returns a pseudorandom `int` in range `[0, max)`.
@@ -221,11 +220,17 @@ pub fn (mut rng PRNG) i64_in_range(min i64, max i64) !i64 {
221220 return min + rng.i64n (max - min)!
222221}
223222
223+ // smallest mantissa with exponent 0 (un normalized)
224+ const reciprocal_2_23rd = 1.0 / f64 (u32 (1 ) << 23 )
225+ const reciprocal_2_52nd = 1.0 / f64 (u64 (1 ) << 52 )
226+ const ieee754_mantissa_f32_mask = (u32 (1 ) << 23 ) - 1 // 23 bits for f32
227+ const ieee754_mantissa_f64_mask = (u64 (1 ) << 52 ) - 1 // 52 bits for f64
228+
224229// f32 returns a pseudorandom `f32` value in range `[0, 1)`
225230// using rng.u32() multiplied by an f64 constant.
226231@[inline]
227232pub fn (mut rng PRNG) f32 () f32 {
228- return f32 ((rng.u32 () >> 9 ) * constants. reciprocal_2_23 rd)
233+ return f32 ((rng.u32 () >> 9 ) * reciprocal_2_23 rd)
229234}
230235
231236// f32cp returns a pseudorandom `f32` value in range `[0, 1)`
@@ -258,15 +263,15 @@ pub fn (mut rng PRNG) f32cp() f32 {
258263 }
259264
260265 // Assumes little-endian IEEE floating point.
261- x = (exp << 23 ) | (x >> 8 ) & constants. ieee754_ mantissa_f32_ mask
266+ x = (exp << 23 ) | (x >> 8 ) & ieee754_ mantissa_f32_ mask
262267 return bits.f32_from_bits (x)
263268}
264269
265270// f64 returns a pseudorandom `f64` value in range `[0, 1)`
266271// using rng.u64() multiplied by a constant.
267272@[inline]
268273pub fn (mut rng PRNG) f64 () f64 {
269- return f64 ((rng.u64 () >> 12 ) * constants. reciprocal_2_52 nd)
274+ return f64 ((rng.u64 () >> 12 ) * reciprocal_2_52 nd)
270275}
271276
272277// f64cp returns a pseudorandom `f64` value in range `[0, 1)`
@@ -298,7 +303,7 @@ pub fn (mut rng PRNG) f64cp() f64 {
298303 if bitcount > 11 {
299304 x = rng.u64 ()
300305 }
301- x = (exp << 52 ) | (x & constants. ieee754_ mantissa_f64_ mask)
306+ x = (exp << 52 ) | (x & ieee754_ mantissa_f64_ mask)
302307 return bits.f64_from_bits (x)
303308}
304309
0 commit comments