Commit cf86db69 authored by Peter Billam's avatar Peter Billam

introduce urandom to access /dev/urandom

parent e8209b90
......@@ -52,6 +52,8 @@ random.ps - Some useful random-number stuff in PostScript
(/home/wherever/ps/lib/random.ps) run
% to send it to a printer see <A HREF="include_run.html">include_run</A> . . .
<A HREF="#urandom">urandom</A> srand
/mean 43.2 def /stddev 8.5 def
{ ...
mean stddev <A HREF="#grand">grand</A> % Gaussian Random
......@@ -83,6 +85,7 @@ for generating random numbers according to various distributions.
<A HREF="#grand">grand</A>, &nbsp;
<A HREF="#irand">irand</A>, &nbsp;
<A HREF="#urand">urand</A>, &nbsp;
<A HREF="#urandom">urandom</A>, &nbsp;
<A HREF="#rayleigh_rand">rayleigh_rand</A>, &nbsp;
<A HREF="#randomget">randomget</A>, &nbsp;
<A HREF="#randomgetn">randomgetn</A>, &nbsp;
......@@ -125,7 +128,22 @@ an Integer from 0 to n-1
<B><A NAME="urand">
urand</A></B>
</DT><DD><P>
This leaves on the stack an number between zero and Unity
This leaves on the stack a number between zero and <B>U</B>nity
</P>
</DD><DT>
<B><A NAME="urandom">
urandom</A></B>
</DT><DD><P>
This reads eight bytes from <I>/dev/urandom</I>
then leaves on the stack an integer between 0 and 2^31 - 1
<BR>
This can be used as a source of real randomness, for example in:<BR>
<CODE> &nbsp; &nbsp; urandom srand</CODE>
<BR>
Of course, it does not work if the file <I>/dev/urandom</I> does not exist;<BR>
for example, it will usually work on a computer,
but will probably not work on a printer :-(
</P>
</DD><DT>
......@@ -217,6 +235,7 @@ www.pjb.com.au/comp/contact.html</A>
</P>
<A NAME="changes"><HR></A><H3>CHANGES</H3><PRE>
20181111 introduce <A HREF="#urandom">urandom</A>
20180712 introduce <A HREF="#zipf_rand">zipf_rand</A>
20170706 introduce <A HREF="#rayleigh_rand">rayleigh_rand</A>
20170512 introduce <A HREF="#urand">urand</A>
......@@ -238,7 +257,10 @@ www.design.caltech.edu/erik/Misc/Gaussian.html</A>
en.wikipedia.org/wiki/Rayleigh_distribution</A>
</LI><LI>
<A HREF="https://en.wikipedia.org/wiki/Zipf%27s_law">
en.wikipedia.org/wiki/Zipf%27s_law</A><BR>
en.wikipedia.org/wiki/Zipf%27s_law</A>
</LI><LI>
<A HREF="http://computer-programming-forum.com/36-postscript/c1b4f734143f2af1.htm">
computer-programming-forum.com/36-postscript/c1b4f734143f2af1.htm</A>
</LI><LI>
<A HREF="lua/randomdist.html">www.pjb.com.au/comp/lua/randomdist.html</A>
</LI><LI>
......
......@@ -14,7 +14,6 @@ usertime realtime add srand
% Published (1981) as CACM Algorithm 448
% http://wwwmaths.anu.edu.au/~brent/pd/rpb023i.pdf
% was replaced in 20170510, fixing some obscure bugs ...
% http://www.design.caltech.edu/erik/Misc/Gaussian.html
% The polar form of the Box-Muller transformation is faster and more robust:
% float x1, x2, w, y1, y2;
......@@ -27,6 +26,7 @@ usertime realtime add srand
% y1 = x1 * w;
% y2 = x2 * w;
% where ranf() obtains a random number uniformly distributed in [0,1]
%
/gauss_rand_already false def
/gauss_rand {
gauss_rand_already {
......@@ -62,6 +62,14 @@ gauss_rand pop % initialise, and discard the resulting zero
rand gauss_rand_r mul
} bind def
/urandom { % random integer between 0 and 2^31 - 1
5 dict begin
/rf (/dev/urandom) (r) file def
rf read pop 256 mul rf read pop add 256 mul rf read pop add
rf read pop 128 mul rf read pop add % remains on stack
rf closefile
} def
/randomget { % usage: [ an array ] randomget
dup length rand exch mod get
} bind def
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment