authorgravatar for erik.arvstedt@gmail.comErik Arvstedt <erik.arvstedt@gmail.com> 2022-05-12 14:13:20+02:00
committergravatar for erik.arvstedt@gmail.comErik Arvstedt <erik.arvstedt@gmail.com> 2022-05-12 15:39:19+02:00
log1d5ea10bee39c6378c5d41597ed477075c3c667f
tree92fa79b1cbaf0eb4673f7c9f69b848ebb733aa41
parent7bedeb9659af84daab33a4804c54b4c8811c0c5d

std.rand: fixup 'improve random float generation'

- Test: Fix bucket counting. Previously, the first hit was not counted. This off-by-one error slightly increased the mean of `*_total_variance`, which decreased the acceptance rate for a particular random seed from 95% to 92.6%. (Irrelevant for test failure because the seed is fixed.) - Improve comments

2 files changed, 8 insertions(+), 6 deletions(-)

lib/std/rand.zig+5-3
...@@ -247,10 +247,9 @@ pub const Random = struct {...@@ -247,10 +247,9 @@ pub const Random = struct {
247247
248 /// Return a floating point value evenly distributed in the range [0, 1).248 /// Return a floating point value evenly distributed in the range [0, 1).
249 pub fn float(r: Random, comptime T: type) T {249 pub fn float(r: Random, comptime T: type) T {
250 // Generate a uniformly random value between for the mantissa.250 // Generate a uniformly random value for the mantissa.
251 // Then generate an exponentially biased random value for the exponent.251 // Then generate an exponentially biased random value for the exponent.
252 // Over the previous method, this has the advantage of being able to252 // This covers every possible value in the range.
253 // represent every possible value in the available range.
254 switch (T) {253 switch (T) {
255 f32 => {254 f32 => {
256 // Use 23 random bits for the mantissa, and the rest for the exponent.255 // Use 23 random bits for the mantissa, and the rest for the exponent.
...@@ -259,6 +258,9 @@ pub const Random = struct {...@@ -259,6 +258,9 @@ pub const Random = struct {
259 const rand = r.int(u64);258 const rand = r.int(u64);
260 var rand_lz = @clz(u64, rand | 0x7FFFFF);259 var rand_lz = @clz(u64, rand | 0x7FFFFF);
261 if (rand_lz == 41) {260 if (rand_lz == 41) {
261 // TODO: when #5177 or #489 is implemented,
262 // tell the compiler it is unlikely (1/2^41) to reach this point.
263 // (Same for the if branch and the f64 calculations below.)
262 rand_lz += @clz(u64, r.int(u64));264 rand_lz += @clz(u64, r.int(u64));
263 if (rand_lz == 41 + 64) {265 if (rand_lz == 41 + 64) {
264 // It is astronomically unlikely to reach this point.266 // It is astronomically unlikely to reach this point.
lib/std/rand/test.zig+3-3
...@@ -336,13 +336,13 @@ test "Random float chi-square goodness of fit" {...@@ -336,13 +336,13 @@ test "Random float chi-square goodness of fit" {
336 if (f32_put.found_existing) {336 if (f32_put.found_existing) {
337 f32_put.value_ptr.* += 1;337 f32_put.value_ptr.* += 1;
338 } else {338 } else {
339 f32_put.value_ptr.* = 0;339 f32_put.value_ptr.* = 1;
340 }340 }
341 var f64_put = try f64_hist.getOrPut(@floatToInt(u32, rand_f64 * @intToFloat(f64, num_buckets)));341 var f64_put = try f64_hist.getOrPut(@floatToInt(u32, rand_f64 * @intToFloat(f64, num_buckets)));
342 if (f64_put.found_existing) {342 if (f64_put.found_existing) {
343 f64_put.value_ptr.* += 1;343 f64_put.value_ptr.* += 1;
344 } else {344 } else {
345 f64_put.value_ptr.* = 0;345 f64_put.value_ptr.* = 1;
346 }346 }
347 }347 }
348348
...@@ -371,7 +371,7 @@ test "Random float chi-square goodness of fit" {...@@ -371,7 +371,7 @@ test "Random float chi-square goodness of fit" {
371 }371 }
372 }372 }
373373
374 // Corresponds to a p-value > 0.05.374 // Accept p-values >= 0.05.
375 // Critical value is calculated by opening a Python interpreter and running:375 // Critical value is calculated by opening a Python interpreter and running:
376 // scipy.stats.chi2.isf(0.05, num_buckets - 1)376 // scipy.stats.chi2.isf(0.05, num_buckets - 1)
377 const critical_value = 1073.6426506574246;377 const critical_value = 1073.6426506574246;