From 4e2e5e8c789caa2b09b32b92896292e3c6dec9bc Mon Sep 17 00:00:00 2001 From: Wouter van Oijen Date: Mon, 24 Jul 2006 20:27:29 +0000 Subject: [PATCH] fix: Splat_random Macro not so random --- docs/changelog/7.x.x.txt | 2 ++ docs/gotcha.txt | 12 ++++++++++++ lib/WebGUI/Macro/Splat_random.pm | 26 ++++++++++++-------------- t/Macro/Splat_random.t | 5 +---- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index c32a4569b..6b028d02a 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -35,6 +35,8 @@ line, which fixed a problem with the WRE monitor, and also enabled us to add more complete connectivity testing. - fix: Templates XHTML compliance (Wouter van Oijen / ProcoliX) + - fix: Splat_random Macro not so random (Wouter van Oijen / ProcoliX) (Thanks + to Colin Kuskie for pointing this out and writing some tests) 7.0.2 diff --git a/docs/gotcha.txt b/docs/gotcha.txt index 390bec935..79fd11917 100644 --- a/docs/gotcha.txt +++ b/docs/gotcha.txt @@ -8,6 +8,18 @@ versions. Be sure to heed the warnings contained herein as they will save you many hours of grief. +7.0.3 +-------------------------------------------------------------------- + + * Numbers generated by the Splat_random macro were not evenly + distributed. It returned numbers between 0 and 'max' (inclusive) + with this two numbers occuring about half as much as other numbers. + This is now fixed, so it will return numbers between 0 and 'max-1' + (inclusive), with an equal probability of any of these numbers. + If you're using this macro, you might need to increment the max + parameter. + + 7.0.2 -------------------------------------------------------------------- diff --git a/lib/WebGUI/Macro/Splat_random.pm b/lib/WebGUI/Macro/Splat_random.pm index c1e982b67..7b05d8446 100644 --- a/lib/WebGUI/Macro/Splat_random.pm +++ b/lib/WebGUI/Macro/Splat_random.pm @@ -21,32 +21,30 @@ Package WebGUI::Macro::Splat_random Macro for returning a bounded, integer random number. +#------------------------------------------------------------------- + =head2 process ( max ) -Random numbers are rounded, not truncated. +Random numbers are truncated to integer values. =head3 max -The maximum random number. If omitted, 1_000_000_000 is +The upper bound for the random number. If omitted, 1_000_000_000 is used as a default. =cut -#------------------------------------------------------------------- sub process { my $session = shift; - my ($temp, @param, $limit); - @param = @_; - if ($param[0] ne "") { - $limit = $param[0]; - } else { - $limit = 1000000000; - } - $temp = round(rand($limit)); - return $temp; + my (@param, $limit); + @param = @_; + if ($param[0] ne "") { + $limit = $param[0]; + } else { + $limit = 1000000000; + } + return int(rand($limit)); } - - 1; diff --git a/t/Macro/Splat_random.t b/t/Macro/Splat_random.t index 77169645b..d1d3f54bb 100644 --- a/t/Macro/Splat_random.t +++ b/t/Macro/Splat_random.t @@ -71,9 +71,6 @@ WHOLE: for (my $i=0; $i<=999; $i++) { ++$bins[$output]; } -is(scalar(@bins), 5, "All bins have values on a sample size of 1000"); +is(scalar(@bins), 4, "All bins have values on a sample size of 1000"); -##Early work in analyzing a frequency distribution showed that the highest -##and lowest bin have half the frequency of center bins. Splat_random doesn't -##seem very random #diag Dumper \@bins;