randomdist.lua - a few simple procedures for generating random numbers.

local R = require 'randomdist'

grand1 = R.new_grand(10,3) grand2 = R.new_grand(100,3) for i = 1,20 do print( grand1(), grand2() ) end gue_irand1 = R.new_gue_irand(4) gue_irand2 = R.new_gue_irand(20) for i = 1,20 do print( gue_irand1(), gue_irand2() ) end for i = 1,20 do print(R.rayleigh_rand(3)) end a = {'cold', 'cool', 'warm', 'hot'} for i = 1,20 do print(R.randomget(a)) end

This module implements in Lua a few simple functions for generating random numbers according to various distributions.

randomdist.lua is based on the PostScript module random.ps

**new_grand()**,
**new_gue_irand()**,
**rayleigh_rand()**,
**randomget()** and
**randomgetn()**

*new_grand*(mean, stddev)-
This function returns a closure, which is a function which you can then call to return a Gaussian (or Normal) Random distribution of numbers with the given

*mean*and*standard deviation*.It keeps some internal local state, but because it is a closure, you may run different Gaussian Random generators simultaneously, for example with different means and standard-deviations, without them interfering with each other.

It uses the algorithm given by Erik Carter in www.design.caltech.edu/erik/Misc/Gaussian.html

This algorithm generates results in pairs, but returns them one by one. Therefore if you are using

*math.randomseed*to reset the random-number generator to a known state, and your code happens to make an odd number of calls to your closure, and you want your program to run consistently, then you should call your closure (eg: grand1) with the argument 'reset' each time you call*math.randomseed*. Eg:`grand1 = R.new_grand(10,3)`

... grand1() ... etc ...

math.randomseed(244823040) ; grand1('reset') *new_gue_irand*(average)-
This function returns a closure, which is a function which you can then call to return a Gaussian-Random-Ensemble distribution of integers.

The Gaussian Unitary Ensemble models Hamiltonians lacking time-reversal symmetry. Considering a hermitian matrix with gaussian-random values; from the ordered sequence of eigenvalues, one defines the normalized spacings

`s = (\lambda_{n+1}-\lambda_n) / <s>`

where <s> = is the mean spacing. The probability distribution of spacings is approximately given by`p_2(s) = (32 / pi^2) * s^2 * e^((-4/pi) * s^2)`

These numerical constants are such that p_2 (s) is normalized: and the mean spacing is 1.`\int_0^\infty ds p_2(s) = 1 \int_0^\infty ds s p_2(s) = 1`

Montgomery's pair correlation conjecture is a conjecture made by Hugh Montgomery (1973) that the pair correlation between pairs of zeros of the Riemann zeta function (normalized to have unit average spacing) is:

`1 - ({sin(pi u)}/{pi u}})^2 + \delta(u)`

which, as Freeman Dyson pointed out to him, is the same as the pair correlation function of random Hermitian matrices. *rayleigh_rand*(sigma)-
This function returns a random number according to the Rayleigh Distribution, which is a continuous probability distribution for positive-valued random variables. It occurs, for example, when random complex numbers whose real and imaginary components are independent Gaussian distributions with equal variance and zero mean, in which case, the absolute value of the complex number is Rayleigh-distributed.

`f(x; sigma) = x exp(-x^2 / 2*sigma^2) / sigma^2 for x>=0`

The algorithm contains no internal state, hence*rayleigh_rand*directly returns a number. *randomget*(an_array)-
This example gets a random element from the given array. For example, the following executes one of the given procedures at random:

`f = {bassclef, trebleclef, sharp, natural} randomget()`

f() *randomgetn*(an_array, n)-
This example returns an array containing

**n**random elements, with distinct indices, from the given array.

This module is available as a LuaRock in luarocks.org/modules/peterbillam so you should be able to install it with the command:

$ su Password: # luarocks install randomdist

or:

# luarocks install http://www.pjb.com.au/comp/lua/randomdist-1.2-0.rockspec

The test script used during development is
www.pjb.com.au/comp/lua/test_randomdist.lua

20171226 1.3 randomgetn allows n == array-size, meaning shuffle the array 20170819 1.2 add randomgetn() 20170707 1.1 grand('reset') more robust, and tested 20170706 1.0 first released version

Peter J Billam, www.pjb.com.au/comp/contact.html

en.wikipedia.org/wiki/Normal_distribution www.design.caltech.edu/erik/Misc/Gaussian.html en.wikipedia.org/wiki/Random_matrix#Gaussian_ensembles en.wikipedia.org/wiki/Random_matrix#Distribution_of_level_spacings en.wikipedia.org/wiki/Montgomery%27s_pair_correlation_conjecture en.wikipedia.org/wiki/Radial_distribution_function en.wikipedia.org/wiki/Pair_distribution_function en.wikipedia.org/wiki/Rayleigh_distribution luarocks.org/modules/luarocks/lrandom www.pjb.com.au/comp/randomdist.html www.pjb.com.au/comp/index.html

Montgomery, Hugh L. (1973), "The pair correlation of zeros of the zeta function", Analytic number theory, Proc. Sympos. Pure Math., XXIV, Providence, R.I.: American Mathematical Society, pp. 181-193, MR 0337821

Odlyzko, A. M. (1987), "On the distribution of spacings between zeros of the zeta function", Mathematics of Computation, American Mathematical Society, 48 (177): 273-308, ISSN 0025-5718, JSTOR 2007890, MR 866115, doi:10.2307/2007890

John Derbyshire, *Prime Obsession*, Joseph Henry Press, 2003, p.288