Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!aero-c!gumby.dsd.trw.com!ondine.dsd.trw.com!suhre From: suhre@ondine.dsd.trw.com (Maurice E. Suhre) Newsgroups: comp.unix.questions Subject: Re: random numbers in awk? Message-ID: <1991May16.012836.25000@gumby.dsd.TRW.COM> Date: 16 May 91 01:28:36 GMT References: <1991Apr24.041134.14519@athena.mit.edu> <1136@mwtech.UUCP> <1991May13.221822.27731@gumby.dsd.TRW.COM> <16154@smoke.brl.mil> Sender: news@gumby.dsd.TRW.COM Reply-To: suhre@ondine.dsd.trw.com.UUCP (Maurice E. Suhre) Organization: TRW Space and Defense Lines: 31 In article <16154@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes: +In article <1991May13.221822.27731@gumby.dsd.TRW.COM> suhre@shark.dsd.trw.com.UUCP (Maurice E. Suhre) writes: +-In article <1136@mwtech.UUCP> martin@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@trwrb.dsd.trw.com