random numbers in awk?
David Fenyes
dfenyes at thesis1.med.uth.tmc.edu
Fri May 24 01:17:02 AEST 1991
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:
>+SysV User's should note that they have a similar program, called random.
>+It's range of output values is restricted to 0..255 (the upper limit must
>+be specified as a calling argument). 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.
The dist. will be changed-- from uniform on 0...255 to uniform
on 0...65535.
The multiplication 'spreads' the pdf of the first call form 1/256 in each
'bin' from 0...255 to 1/256 in every 256'th bin from 0 to 65280.
Addition of the second number convolves its pdf (0..255) with the first,
'filling out' the summed pdf to a unform distribution of 1/65536 from
0...65535.
If the modulo is a power of 2, then the distribution of the pdf below
the mod value will be uniform. (The distribution above the value will
be perfectly mapped to the dist. below so that each 'bin' gets the same
amount of added probability). If the mod value is NOT a power of 2, so
that 633536 MOD n != 0, then the dist will be:
from 0 to 65536 % n - 1: ([65536 / n] + 1) / 65536
from 65536 % n up: ([63336 / n]) / 65536
where [x] means the greatest integer <= x.
Regards,
David
---------
David Fenyes dfenyes at thesis1.med.uth.tmc.edu
University of Texas Medical School Houston, Texas
More information about the Comp.unix.questions
mailing list