Re: Random integer formula?

From: Top Spin (ToppSpin_at_hotmail.com)
Date: 12/03/04


Date: Fri, 03 Dec 2004 08:03:50 -0800

On Fri, 3 Dec 2004 06:15:01 -0800, "Bonj"
<Bonj@discussions.microsoft.com> wrote:

>> your formula will not return 7
>
>Wrong.
>My original formula's correct, apart from the banker's rounding issue that
>Rick pointed out, for which you would need to use CInt, but like he says, I'm
>not losing any sleep over it.

Your formula does not work. Period.

The rounding effect is one problem, but a minor one.

The fatal problem is the one I originally pointed out; namely, that
you divide by (max-min) instead of (max-min+1). There are max-min+1
integers in (min,max). You must divide the random number on [0,1) by
that number in order to get equal intervals.

The rounding effect only serves to mask the problem. It allows your
formula tol generate all of the integers on (min,max), but not in
equal proportions -- except when max-min=2.

If min=0 and max=2, your formula produces the following results:

Rnd() Result
   [0-.25] 0
(.25-.75) 1
[.75-1) 2

Your formula will return "1" 50% of the time. Hardly an even
distribution.

A quick simulation of 1,000,000 trials results in:

min=0, max=2, n=1,000,000, ave=333,333
  N Tally Delta %
  0 249,860 -83,473 -25.04%
  1 500,814 167,481 50.24%
  2 249,326 -84,007 -25.20%

This is a random distribution, but hardly even.

We get similar results for other domains:

min=0, max=3, n=1,000,000, ave=250,000
  N Tally Delta %
  0 167,308 -82,692 -33.08%
  1 333,202 83,202 33.28%
  2 332,872 82,872 33.15%
  3 166,618 -83,382 -33.35%

min=0, max=9, n=1,000,000, ave=100,000
  N Tally Delta %
  0 55,749 -44,251 -44.25%
  1 111,709 11,709 11.71%
  2 111,052 11,052 11.05%
  3 111,006 11,006 11.01%
  4 111,273 11,273 11.27%
  5 110,778 10,778 10.78%
  6 111,046 11,046 11.05%
  7 110,520 10,520 10.52%
  8 111,300 11,300 11.30%
  9 55,567 -44,433 -44.43%

The good news is that is works properly only if max-min=2:

min=0, max=1, n=1,000,000, ave=500,000
  N Tally Delta %
  0 501,036 1,036 0.21%
  1 498,964 -1,036 -0.21%

--
Running MS VB 6.0 Pro (SP5)
PC: HP Omnibook 6000
OS: Win 2K SP-4 (5.00.2195)
Email: Usenet-20031220 at spamex.com
(11/03/04)