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


 (/home/wherever/ps/lib/random.ps) run
 % to send it to a printer see include_run . . .

 /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


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

PROCEDURES :     grand,   irand,   urand,   rayleigh_rand,   randomget,   randomgetn

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


This leaves on the stack an number between zero and Unity

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.


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
    curl http://www.pjb.com.au/comp/free/random.ps.txt -o random.ps

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


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


 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


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