# random.ps

### NAME

random.ps - Some useful random-number stuff in PostScript

### SYNOPSIS

``` (/home/wherever/ps/lib/random.ps) run
% if run fails with invalidaccess you may need to use  gv --nosafer
% to send it to a printer see include_run . . .

urandom srand

/mean 43.2 def  /stddev 8.5 def
{  ...
mean stddev grand   % Gaussian Random
...  i n gt { exit } if
} loop

n irand  % random integer 0 .. n-1

urand  % random number between 0.0 and 1.0

sigma rayleigh_rand  % Rayleigh Distribution

[ an array ] randomget   % random element
[ an array 5 ] randomgetn   % 5 random elements

[ an array ] zipf_rand   % Zipf distribution
```

### DESCRIPTION

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

### PROCEDURES :     grand,   irand,   urand,   urandom,   rayleigh_rand,   randomget,   randomgetn,   zipf_rand

mean standarddeviation grand

This example leaves on the stack, a number from a Gaussian (or Normal) random distribution with the given mean and standard deviation.

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  `srand`  to reset the random-number generator to a known state, and your code happens to make an odd number of calls to  `grand`,  and you want the page to display consistently on reload, then you should set
`/gauss_rand_already false def`
each time you call  `srand`.

n irand

This example leaves on the stack an Integer from 0 to n-1

urand

This leaves on the stack a number between zero and Unity

urandom

This reads eight bytes from /dev/urandom then leaves on the stack an integer between 0 and 2^31 - 1
This can be used as a source of real randomness, for example in:
`     urandom srand`
Of course, it does not work if the file /dev/urandom does not exist;
for example, it will usually work on a computer, but will probably not work on a printer :-(

sigma rayleigh_rand

This function leaves on the stack 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 ```

[ an array ] randomget

This example gets a random element from the given array.
For example, the following executes one of the given procedures at random:
`[ /bassclef /trebleclef /sharp /natural ] randomget cvx exec`

[ an array ] 5 randomgetn

This example leaves on the stack an array containing five random elements from the given array.
Those elements have distinct indices in the given array, so that if the elements of the given array are all distinct, the elements of the returned array will also be distinct.

[ an array ] zipf_rand

This example leaves on the stack an element from the given array according to the Zipf distribution
en.wikipedia.org/wiki/Zipf%27s_law
with the first element being the most frequent.

### INSTALL

To install: go to www.pjb.com.au/comp/free/random.ps.txt and save the file to your local disc.
Rename it to random.ps and move it into some appropriate directory such as `~/ps/lib` . . .

Or, first change directory to where you keep your PostScript libraries:
```     cd /home/wherever/ps/lib/ ```
(or wherever)   and then either:
```     wget -O random.ps http://www.pjb.com.au/comp/free/random.ps.txt ```
or:
```     curl http://www.pjb.com.au/comp/free/random.ps.txt -o random.ps ```

Or, get it from gitlab:
```     git clone https://gitlab.com/peterbillam/postscriptlib ```

### AUTHOR

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

### CHANGES

``` 20181111 introduce urandom
20180712 introduce zipf_rand
20170706 introduce rayleigh_rand
20170512 introduce urand
20170511 the old code deleted; much neater
20170510 grand gets a new algorithm, fixing some obscure bugs
20160501 first released version
```

### SEE ALSO

Back to P J B Computing or to www.pjb.com.au . . .