From: Jim Weigang <jimw>
To: apl-l@hermes.csd.unb.ca (comp.lang.apl)
Date: Wed, 25 Mar 1998 21:25:06 -0500 (EST)
Subject: Re: Monadic Roll and generating normal distributions in APL

Tony Corso wrote on March 23:

> I find myself in need of generating a bunch of numbers from a normal
> distribution with mean = 0 and std. dev. = 1. And I need to do it in APL.

Here's an APL program that does this (and actually works):




(Note: The program assumes #IO is 1.  Localize and set #IO if you're in
the habit of fiddling with the index origin.)

   As Tony deduced, uniform random numbers in a range such as [0..1] or
[-1..1] are produced by arithmetically transforming the results of roll.
The technical detail he didn't know was that the roll algorithm used by
most APL systems works with 32-bit signed integers, so the finest
resolution for floating randoms can be obtained by transforming ?M,
where M is the largest integer value, (2*31)-1.

   For an explanation of why APL's roll function works well (and the
significance of 16807), see the article "How the Roll Function Works",
by E. E. McDonnell, in Quote Quad vol. 8 no. 3 [1978].  Another
interesting article is "Merrily We Roll Along", by J. O. Shallit, in the
the APL83 Conference Proceedings.  He describes a way of using the
current value of #RL to figure out how many random numbers have been
generated.  In the first appendix, I was surprised to find a description
of the infamous Chinese Remainder Theorem (of ACM ball clock puzzler 
fame).

                                                Jim


Note: The APL code on this page can be extracted without you having to retype it.
See "Downloading Code" for instructions.





JimW's Home Page