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

 [ an array ] randomget   % random element


This module implements in PostScript a few simple procedures for using random numbers.

PROCEDURES :     grand,   irand,   urand,   randomget

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.   Thus, the density is     f(x) = exp(.5 * x**2) / sqrt(2 * pi)

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

n irand

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


This leaves on the stack an number between 0.0 and 1.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


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


 20170512 introduce urand
 20170511 the old code deleted; much neater
 20170510 gauss_rand gets a new algorithm, fixing some obscure bugs
 20160501 first released version


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