random numbers in awk?

Maurice E. Suhre suhre at ondine.dsd.trw.com
Thu May 16 11:28:36 AEST 1991


In article <16154 at smoke.brl.mil> gwyn at smoke.brl.mil (Doug Gwyn) writes:
+In article <1991May13.221822.27731 at gumby.dsd.TRW.COM> suhre at shark.dsd.trw.com.UUCP (Maurice E. Suhre) writes:
+-In article <1136 at mwtech.UUCP> martin at mwtech.UUCP (Martin Weitzel) writes:
+-+If you need larger numbers, you can, of course, call random twice, multiply
+-+the result of the first call by 256, add the result of the second call, and
+-+take the whole thing modulo the largest number you want. 
+-Be advised that the operations mentioned (multiplication, modulo) will,
+-in general, change the distribution of the random numbers.  That is, if you
+-started with uniformly distributed random numbers, then what you will
+-come out with will not be uniformly distributed.
+
+Be advised that the procedure Martin suggested does preserve uniformity.

There seems to have been a shortage of correct analysis on my part, and
perhaps elsewhere.  Yes, the multiplication as described above does retain
the property of uniformity of distribution (assuming that it was uniform
going in).  No, the modulo strategy does not preserve uniformity.  An
example for those who are getting confused or have misunderstood:

Suppose that you have a uniform random number generator which generates
the integers 0, 1, 2,...,9.  Then we have p(0) = p(1) ... p(9) = 0.1
Now, suppose we take these numbers modulo 7.  Then the numbers 7, 8, and 9
are mapped into 0, 1, and 2 respectively.  The new probabilities become
p(0) = p(1) = p(2) = 0.2, and p(3) ... p(6) = 0.1 as before.  Any modulus
operation other than by 2 will result in a non-uniform distribution.  The
solution is to scale all numbers by the same factor, either compressing 
or expanding the range.  If integers are needed, then you could toss
out any random numbers which are outside the range of interest.
-- 
Maurice Suhre
suhre at trwrb.dsd.trw.com



More information about the Comp.unix.questions mailing list