?

Log in

No account? Create an account
entries friends calendar profile SpaceParanoids.net Previous Previous Next Next
Programming Question of the Day - Jarrett Heather
Jarrett Heather's Journal
jarrett
jarrett
Programming Question of the Day
8 comments or Leave a comment
Comments
macklinr From: macklinr Date: January 25th, 2005 06:56 pm (UTC) (Link)
RND returns a value between zero and one, but that's not an inclusive "between".

You won't get a 1 from RND, thus int(rnd * i) will never equal i, or your last term (the reason you get 0 is because int truncs the floating point part, which can result in 0). You might want to try "int(rnd * (i + 1))". This will increase your upper random limit without giving up 0.
jarrett From: jarrett Date: January 25th, 2005 07:46 pm (UTC) (Link)
Yes, that was exactly the bug. It was hardly noticable in the results, but the last item was never picked first, then the next to last was never picked second, and so on.

Because I'm a perfectionist, I wrote a routine to run the system x number of times and track the randomness of the results:

100 Loops:
                                   Position
       0      1      2      3      4      5      6      7      8      9   
0   12.50%  8.33% 11.11% 10.00% 11.11%  5.88% 16.67% 20.00%  7.69%  9.09%
1   14.29% 11.11% 12.50%  7.14%  9.09% 16.67%  7.14%  5.56% 14.29% 16.67%
2   14.29%  9.09% 20.00% 16.67%  7.69% 14.29%  8.33%  8.33%  9.09%  6.25%
3    6.67% 14.29% 14.29%  5.88% 11.11% 10.00% 12.50% 12.50% 12.50%  9.09%
4   20.00% 11.11% 11.11% 12.50% 12.50% 14.29%  7.69%  5.26%  6.67% 14.29%
5    6.25% 12.50%  8.33% 20.00%  6.25% 16.67% 11.11% 12.50%  8.33% 12.50%
6   14.29% 14.29%  5.00%  9.09% 25.00%  8.33% 10.00% 20.00% 10.00%  7.14%
7    7.14%  8.33%  8.33% 12.50%  9.09%  7.14% 20.00% 12.50% 25.00%  8.33%
8    8.33% 14.29% 10.00%  6.25%  8.33%  9.09% 11.11% 16.67% 10.00% 14.29%
9   11.11%  5.56% 12.50% 20.00% 14.29% 10.00%  7.14%  9.09% 10.00% 12.50%

Of course, the law of large numbers tells us we'll get a lot closer to 10% if we keep going.

Ten million loops:
                                   Position
       0      1      2      3      4      5      6      7      8      9   
0   10.01% 10.00% 10.00% 10.00% 10.00% 10.00% 10.00%  9.99% 10.00% 10.00%
1   10.00% 10.00%  9.99% 10.00% 10.00% 10.00% 10.00% 10.01% 10.00% 10.00%
2   10.00% 10.00% 10.00%  9.99% 10.00% 10.00% 10.00% 10.00% 10.00% 10.00%
3   10.00% 10.01% 10.00% 10.00%  9.99% 10.00% 10.00% 10.00% 10.01% 10.00%
4   10.00% 10.00% 10.00% 10.00% 10.01%  9.99% 10.00% 10.00% 10.00% 10.00%
5   10.00% 10.00% 10.00% 10.00%  9.99% 10.00% 10.00% 10.00% 10.00% 10.00%
6    9.99% 10.00% 10.00% 10.00% 10.00% 10.00% 10.01% 10.00%  9.99% 10.00%
7   10.00% 10.00% 10.00% 10.00% 10.00% 10.01% 10.00% 10.00% 10.00% 10.00%
8   10.00% 10.00% 10.00%  9.99% 10.00%  9.99%  9.99% 10.00% 10.01% 10.01%
9    9.99% 10.00% 10.01% 10.00% 10.00% 10.00% 10.00% 10.01%  9.99% 10.00%

That's a more even distribution than I expected. I guess that's how casinos stay in business, eh?
From: dark_wolfe Date: January 26th, 2005 01:13 am (UTC) (Link)
This just shows you have WAY too much time on your hands! :-)

(but kudos,nonetheless)
8 comments or Leave a comment