home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume28 / mrandom-3.0 / part04 < prev    next >
Encoding:
Text File  |  1994-05-06  |  58.8 KB  |  1,584 lines

  1. Newsgroups: comp.sources.unix
  2. From: cthombor@theory.lcs.mit.edu (Clark D. Thomborson)
  3. Subject: v28i030: mrandom-3.0 - random number generator with persistent state, Part04/06
  4. References: <1.768285901.18944@gw.home.vix.com>
  5. Sender: unix-sources-moderator@gw.home.vix.com
  6. Approved: vixie@gw.home.vix.com
  7.  
  8. Submitted-By: cthombor@theory.lcs.mit.edu (Clark D. Thomborson)
  9. Posting-Number: Volume 28, Issue 30
  10. Archive-Name: mrandom-3.0/part04
  11.  
  12. #! /bin/sh
  13. # This is a shell archive.  Remove anything before this line, then unpack
  14. # it by saving it into a file and typing "sh file".  To overwrite existing
  15. # files, type "sh file -c".  You can also feed this as standard input via
  16. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  17. # will see the following message at the end:
  18. #        "End of archive 4 (of 6)."
  19. # Contents:  doc/mrandom.tex
  20. # Wrapped by vixie@gw.home.vix.com on Fri May  6 21:42:56 1994
  21. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  22. if test -f 'doc/mrandom.tex' -a "${1}" != "-c" ; then 
  23.   echo shar: Will not clobber existing file \"'doc/mrandom.tex'\"
  24. else
  25. echo shar: Extracting \"'doc/mrandom.tex'\" \(56912 characters\)
  26. sed "s/^X//" >'doc/mrandom.tex' <<'END_OF_FILE'
  27. X% mrandom.tex 3.1 5/28/93
  28. X\documentstyle[titlepage,latexinfo]{article}
  29. X\pagestyle{empty}
  30. X
  31. X\begin{document}
  32. X\begin{center}
  33. X\vspace{1in}
  34. X{\large
  35. XMassachusetts Institute of Technology\\
  36. X}
  37. X77 Massachusetts Avenue \\
  38. XCambridge, MA 02139 \\
  39. X\end{center}
  40. X
  41. X\vspace{1in}
  42. X\begin{center}
  43. X{\Large\bf
  44. Xmrandom 3.0 User's Manual}
  45. X\end{center}
  46. X\vskip .1 in
  47. X\begin{center}
  48. Xby
  49. X\end{center}
  50. X\vskip .1in
  51. X\begin{center}
  52. X{\large \em Robert Plotkin}
  53. X\vskip .5 in
  54. XDepartment of Electrical Engineering and Computer Science
  55. X\vskip 3.0 in
  56. XMay 1993
  57. X\end{center}
  58. X
  59. X\clearpage
  60. X\pagestyle{headings}
  61. X\pagenumbering{roman}
  62. X\tableofcontents
  63. X\clearpage
  64. X\listoffigures
  65. X\clearpage
  66. X\pagenumbering{arabic}
  67. X
  68. X\section{Introduction}
  69. X
  70. XThe mrandom package contains a library of routines for using random
  71. Xnumber generators (RNGs) in C in the Unix 4.3bsd environment.  This
  72. XUser's Manual is designed as a guide for programmers who wish to use the
  73. Xmrandom package within their own programs.  The current version of
  74. Xmrandom (version 3.0) is a major rewrite of mrandom version 2.1,
  75. Xreleased by Clark Thomborson in July 1992 (available via anonymous
  76. X\code{ftp} from \code{theory.lcs.mit.edu}).  The package now provides:
  77. X
  78. X\begin{itemize}
  79. X\item A standardized interface to many simultaneously-active RNGs,
  80. X\item A standardized, and unbiased, method for generating random
  81. Xintegers in the range 0..\code{m}-1,
  82. X\item A standardized method for generating floating point numbers
  83. Xuniformly distributed in [0.0, 1.0),
  84. X\item Two standardized methods for generating pseudorandom bit
  85. Xstreams,
  86. X\item Time-efficient vectorized calls, returning multiple uniform
  87. Xvariates,
  88. X\item Buffered and unbuffered calls for efficient generation of
  89. Xpseudorandom generates in both large and small quantities,
  90. X\item The ability to ``split'' RNGs to produce parallel output streams
  91. Xusing the ``leapfrog'' method,
  92. X\item A shorthand notation for completely specifying the
  93. Xalgorithm and current state of an RNG, in an 80-character human-
  94. Xreadable ASCII string,
  95. X\item A method for reconstructing an RNG state from its shorthand
  96. Xnotation,
  97. X\item A standardized method for adding new RNGs to the package,
  98. Xand
  99. X\item A file-I/O interface allowing fast saves and restarts of RNG state
  100. Xvectors.
  101. X\end{itemize}
  102. X
  103. XYou can obtain the complete distribution of mrandom by anonymous ftp
  104. Xfrom \code{theory.lcs.mit.edu}.  Take the file \file{mrandom.tar.Z} from
  105. Xthe directory \code{/pub/cthombor/Mrandom/V3.0}.  The distribution
  106. Xcontains all source code, instructions, and this manual.
  107. X
  108. XIn addition, the mrandom package includes an unsupported set of routines
  109. Xfor testing the statistical properties of the RNGs in the package.  The
  110. Xroutines are packaged in an executable called \code{mrtest}.
  111. XInformation about \code{mrtest} is available in the \code{doc/mrtest}
  112. Xdirectory of the distribution.  Although \code{mrtest} is included in
  113. Xthe current distribution, it is not supported.
  114. X
  115. XQuestions, comments, bug reports, etc. should be sent to Clark
  116. XThomborson at \code{cthombor@ub.d.umn.edu}.
  117. X
  118. X\section{Files in the Distribution Directory}
  119. XThe mrandom source code distribution includes the following
  120. Xfiles:
  121. X
  122. X\begin{description}
  123. X\item[makefile] The makefile for creating the \code{mrtest} program and the \code{mrandom.a}
  124. Xlibrary.
  125. X\item[README] General information about the mrandom package, including
  126. Xchanges to the last version.
  127. X\item[mrandom.c mrandom.h] The source and header files for the main
  128. Xmrandom module.
  129. X\item[bentley.c bentley.h] The source and header files for Bentley's
  130. X  version of the generator described in Knuth Vol 2, Section 3.6.
  131. X\item[pcrand.c pcrand.h] The source and header files for the Portable Combined
  132. XRNG.
  133. X\item[ran0.c ran0.h ran1.c ran1.h ran2.c ran2.h] The source and header
  134. Xfiles for Press and Teukolsky's ran0, ran1, and ran2.
  135. X\item[ultra.c ultra.h] The source and header files for Marsaglia's Ultra
  136. Xgenerator.
  137. X\item[mrtest.c] The mrtest source file.
  138. X\item[xsq.c xsq.h] Code used by mrtest.
  139. X\item[rngs.h] The header file for the UNIX RNGs and the trivial RNG.
  140. X\item[newrng.c newrng.h] Source and header file templates for a new RNG.
  141. X\item[mrandom.3] The man pages for mrandom.
  142. X\item[mrtest.1] The man pages for mrtest.
  143. X\item[script] A test script for mrtest.
  144. X\item[mrandom.tex] The latex source for this manual.
  145. X\item[latexinfo.sty] The style file needed to latex this manual.
  146. X\item[mrandom.txt] Plain ASCII text version of this manual.
  147. X\item[mrandom.ps] PostScript version of this manual.
  148. X\end{description}
  149. X
  150. X\section{Installing mrandom}
  151. X\label{sec:install}
  152. XPreparing the mrandom package for use by other programs is
  153. Xsimple.  Merely position yourself in the directory which contains the
  154. Xmrandom files and type:\\
  155. X\begin{example}
  156. Xmake all\\
  157. X\end{example}
  158. XThis will compile all necessary source files and create the
  159. X\code{mrandom.a} library, as well as the \code{mrtest} binary executable.
  160. X
  161. XYou can also make either the \code{mrandom.a} library or the \code{mrtest}
  162. Xexecutable by typing:\\
  163. X\begin{example}
  164. Xmake mrandom.a\\
  165. X\end{example}
  166. Xor\\
  167. X\begin{example}
  168. Xmake mrtest\\
  169. X\end{example}
  170. X\noindent
  171. Xrespectively.
  172. X
  173. XTo save disk space, various intermediate object files can be removed
  174. Xwith:\\
  175. X\begin{example}
  176. Xmake clean\\
  177. X\end{example}
  178. XThe system can be restored to its original state with:\\
  179. X\begin{example}
  180. Xmake realclean\\
  181. X\end{example}
  182. XThe mrandom package is written in ANSI C, and should be
  183. Xeasily portable to any UNIX-based system supporting a C language
  184. Xcompiler.  However, when compiling on a new system, confirm that
  185. Xthe \code{long} type is represented in 32 bits.
  186. X
  187. X\section{What is an RNG?}
  188. X
  189. XWithin the mrandom package, an RNG is represented by the \code{RNGdata}
  190. Xstructure.  An RNG has several abstract characteristics which are
  191. Xdescribed in this section.  For a more detailed description of the
  192. Xrepresentation of the \code{RNGdata} structure in C, see
  193. XAppendix \ref{app:rngdata}.
  194. X
  195. XAssociated with every RNG is:
  196. X\begin{itemize}
  197. X\item an algorithm number, which determines which algorithm the RNG
  198. Xuses to produce pseudorandom generates.  For descriptions of the
  199. Xalgorithms which are currently installed in the package, see Appendix
  200. X\ref{app:installedrngs}.
  201. X\item an ``mrandom algorithm number,'' which determines which
  202. Xalgorithm the \code{mrandomrv} routine will use to produce
  203. Xrestricted-range integers when called with the RNG.  (See the
  204. Xdescription of \code{mrandomrv} in Section \ref{sec:mrandomrv} for more
  205. Xinformation.)
  206. X\item a state vector, which contains the current state of the RNG.
  207. X\item the size of the state vector.
  208. X\item a seed vector, which contains the seeds which were used to
  209. Xseed the RNG.
  210. X\item the size of the seed vector.
  211. X\item a count of the number of times the RNG has been called since
  212. Xit was initialized.
  213. X\item two buffers
  214. X\begin{itemize}
  215. X\item a bit buffer for bit generates
  216. X\item a main buffer for all other generates.
  217. X\end{itemize}
  218. XSee Section \ref{sec:genproc} for a description of how the two buffers work.
  219. X\item a split value, which determines how many generates to skip
  220. Xbetween values which are returned from the RNG.  For a detailed
  221. Xdescription of the split value, see Section \ref{sec:examandmodify}.
  222. X\item a string containing the human-readable name of the RNG.
  223. X\item the range of the RNG, such that the RNG is capable of
  224. Xproducing integers with a maximum value of range-1.
  225. X\end{itemize}
  226. X
  227. X\section{Using the mrandom package}
  228. X
  229. X\subsection{Overview of the Library}
  230. XThis section describes the mrandom library.  The procedures
  231. Xin the library are gathered into the following groups:
  232. X\begin{itemize}
  233. X\item functions for initializing and de-initializing (killing)
  234. XRNGs
  235. X\item functions for returning generates from RNGs
  236. X\item functions for saving and restarting RNGs
  237. X\item a function for seeding RNGs
  238. X\item a function for checking the integrity of RNG state vectors
  239. X\item a function for producing a human-readable ASCII description
  240. Xof an RNG
  241. X\item functions for examining and modifying characteristics of RNGs
  242. X\end{itemize}
  243. X
  244. X\subsection{Return codes from mrandom routines}
  245. XMost mrandom routines follow one of two conventions for return
  246. Xvalues.
  247. X
  248. X\noindent
  249. XFor those routines which return pointers to an \code{RNGdata}
  250. Xstructure:
  251. X\begin{itemize}
  252. X\item A valid pointer to an \code{RNGdata} structure is returned upon
  253. Xsuccess.
  254. X\item A null pointer is returned upon failure.
  255. X\end{itemize}
  256. XFor routines which produce vectors of pseudorandom generates:
  257. X\begin{itemize}
  258. X\item The first cell of the vector is returned upon success.
  259. X\item The program aborts upon error.
  260. X\end{itemize}
  261. XFor all other routines:
  262. X\begin{itemize}
  263. X\item A 0 or -1 is returned upon failure.
  264. X\end{itemize}
  265. XDetails are provided in the description of the mrandom library
  266. Xin Section \ref{sec:library}.
  267. X
  268. X\subsection{Linking}
  269. XIn order to use the mrandom library of routines in your programs
  270. Xyou must:
  271. X\begin{itemize}
  272. X\item Link the \code{mrandom.a} library with your program.  This will
  273. Xtypically involve merely including the library on your compiler's
  274. Xcommand line, such as:\\
  275. X\begin{example}
  276. Xcc myprog.c mrandom.a\\
  277. X\end{example}
  278. XSince mrandom uses some UNIX mathematical functions, you may also need
  279. Xto link a math library with your program, as in the following:\\
  280. X\begin{example}
  281. Xcc myprog.c mrandom.a -lm\\
  282. X\end{example}
  283. XCheck the documentation for your C compiler for details.
  284. X\item Include the following line at the top of your source file:
  285. X\begin{example}
  286. X#include "mrandom.h"
  287. X\end{example}
  288. X\end{itemize}
  289. X
  290. X\subsection{The mrandom library}
  291. X\label{sec:library}
  292. X
  293. X\subsubsection{Initialization and De-Initialization}
  294. X\label{sec:initkill}
  295. X
  296. XIn order to use an RNG, it must first be initialized.  This
  297. Xis accomplished by first declaring a pointer to an \code{RNGdata}
  298. Xstructure, and then calling \code{init_rng}, which allocates memory for
  299. Xthe RNG and readies it for use by the other routines in the
  300. Xpackage.\\
  301. X
  302. X\begin{example}
  303. X    RNGdata *init_rng(alg,mrandom_alg,seed,count1,count2,bufsize)
  304. X    long alg;
  305. X    long mrandom_alg;
  306. X    long *seed;
  307. X    long count1, count2;
  308. X    long bufsize;
  309. X
  310. X    int kill_rng(rng)
  311. X    RNGdata *rng;\\
  312. X\end{example}
  313. X
  314. X\code{init_rng} returns a pointer to an initialized RNG.  A pointer
  315. Xreturned by \code{init_rng} is valid for use by all other mrandom routines.
  316. X
  317. X\code{alg} is the number of the algorithm to be used by the RNG.  (See
  318. XAppendix \ref{app:installedrngs}.)
  319. X
  320. X\code{mrandom_alg} is the algorithm to be used by \code{mrandomrv} when
  321. Xcalled with the RNG.  (See Section \ref{sec:mrandomrv}.)
  322. X
  323. X\code{seed} is a pointer to a seed vector to be used to seed the RNG.
  324. X(See Section \ref{sec:seedproc}.)
  325. X
  326. X\code{count1} and \code{count2} determine the number of initial values
  327. Xto be generated by the RNG and then discarded, according to the formula:\\
  328. X
  329. Xnumber to discard = \code{count1 + BILLION*count2},\\
  330. X
  331. X where \code{BILLION} is defined in \file{mrandom.h} as the decimal
  332. Xconstant 1000000000.
  333. X
  334. X\code{bufsize} is the size of the RNG's main buffer.  A non-positive value of
  335. X\code{bufsize} will be interpreted as a value of 1.  (See Section
  336. X\ref{sec:genproc} for more information on buffering.)
  337. X
  338. X\code{kill_rng} destroys the RNG, making it invalid for use.  This
  339. Xprocedure de-allocates the space used by the RNG, and should therefore
  340. Xbe used to kill RNGs which will no longer be used.
  341. X
  342. XDo {\em not} use an \code{RNGdata} pointer which points to an active RNG
  343. Xto store the return value of \code{init_rng}.  In order to initialize an
  344. XRNG, you should either:
  345. X\begin{itemize}
  346. X\item Declare a new \code{RNGdata} pointer, and then use it to store the
  347. Xreturn value of \code{init_rng}, as shown in Figure \ref{fig:properinit}.
  348. X
  349. X\begin{figure}
  350. X\begin{example}
  351. XRNGdata *rng;
  352. Xlong seed[1];
  353. X
  354. Xrng=init_rng(2, 0, seed, 10000, 0, 8192);
  355. X\end{example}
  356. X\caption{Proper initialization of an RNG}
  357. X\label{fig:properinit}
  358. X\end{figure}
  359. X
  360. X\item Use \code{kill_rng} to de-initialize an \code{RNGdata} pointer
  361. Xwhich points to an active RNG, and then use that pointer to store to the
  362. Xreturn value of \code{init_rng}, as shown in Figure
  363. X\ref{fig:properreinit}.  Figure \ref{fig:improperreinit} shows how an
  364. XRNG should {\em not} be re-initialized.
  365. X\begin{figure}
  366. X\begin{example}
  367. XRNGdata *myrng;
  368. Xlong seed[1];
  369. X
  370. Xseed[0]=12345;
  371. Xmyrng=init_rng(2, 0, seed, 10000, 0, 8192);
  372. Xkill_rng(myrng);
  373. Xmyrng=init_rng(3, 0, seed, 5000, 0, 2048);
  374. X\end{example}
  375. X\caption{Proper re-initialization of an RNG}
  376. X\label{fig:properreinit}
  377. X\end{figure}
  378. X\end{itemize}
  379. X
  380. X\begin{figure}
  381. X\begin{example}
  382. XRNGdata *myrng;
  383. Xlong seed[1];
  384. X
  385. Xseed[0]=12345;
  386. Xmyrng=init_rng(2, 0, seed, 10000, 0, 8192);
  387. Xmyrng=init_rng(3, 0, seed, 5000, 0, 2048);
  388. X\end{example}
  389. X\caption{Improper re-initialization of an RNG}
  390. X\label{fig:improperreinit}
  391. X\end{figure}
  392. X
  393. X\subsubsection{Procedures for Generating Pseudorandom Numbers}
  394. X\label{sec:genproc}
  395. XAn initialized RNG can be used to generate pseudorandom
  396. Xnumbers by using a variety of routines described in this section.
  397. X
  398. XRoutines are provided for producing generates of four types:
  399. X\begin{tex}
  400. X\begin{itemize}
  401. X\item{double precision floating point generates in the range [0,1)}
  402. X\item{single precision floating point generates in the range [0,1)}
  403. X\item{long integer generates in the range $0..r-1$, where r is the range
  404. Xof the RNG being used to produce generates}
  405. X\item{long integer generates in the range $0..\code{m}-1$, for any $1
  406. X\leq \code{m} < \code{range\_rng(rng)}$}
  407. X\end{itemize}
  408. X\end{tex}
  409. X
  410. XNote that although both single and double precision floats can be
  411. Xreturned by the generate-producing routines, the actual precision of the
  412. Xgenerates produced is determined by the precision of the underlying
  413. Xgenerator being used.  In other words, the difference between routines
  414. Xwhich return generates of type \code{double} and those which return
  415. Xgenerates of type \code{float} is merely in the ``packaging'' of the
  416. Xgenerates, not in the precision they provide.  Information about the
  417. Xprecision of the RNGs currently installed in the package is in Appendix
  418. X\ref{app:installedrngs}; such information can also be obtained at
  419. Xrun-time through the \code{range_rng} routine (see Section
  420. X\ref{sec:examandmodify}).  The current version of the package only
  421. Xsupports RNGs with precisions of no more than 32 bits.
  422. X
  423. XBoth buffered and unbuffered routines are provided.  Unbuffered routines
  424. Xcall the underlying RNG only as many times as are needed to produce the
  425. Xrequested number of generates, while buffered routines maintain buffers
  426. Xof generates, so that generates may be produced efficiently even when
  427. Xrequested in small quantities.  Roughly, buffered routines are
  428. Xpreferable when generates are requested one at a time or in small
  429. Xquantities, while unbuffered routines are preferable when generates are
  430. Xrequested in large quantities.  Some other differences between buffered
  431. Xand unbuffered routines are discussed later in this section.  The size
  432. Xof the buffer used by an RNG is determined at the time of the RNG's
  433. Xinitialization; effective buffer sizes will vary from application to
  434. Xapplication.
  435. X
  436. XThe name of a routine denotes the type of the value which the routine
  437. Xreturns and whether the routine is buffered or unbuffered. The first
  438. Xletter of a routine denotes the type of value which it returns: ``d''
  439. Xfor double precision and ``f'' for single precision floating point in
  440. Xthe range [0,1); ``l'' for long integer in the range
  441. X0..(\code{range_rng(rng)}-1), and ``b'' for bit (either a 0 or a 1).  If
  442. Xthe second letter of the routine's name is an ``x'', then the routine is
  443. Xunbuffered.  Otherwise, the routine is buffered.
  444. X
  445. XFor convenience in user programming, we also provide a number of macros
  446. Xthat supply default parameter values.  The last two letters of all our
  447. Xfundamental routines is ``rv''.  This means that they must be provided
  448. Xwith both a pointer to an \code{RNGdata} structure and a vector to fill
  449. Xwith generates from the RNG.  Macros whose names do not contain an ``r''
  450. Xhave the \code{RNGdata} pointer omitted from their parameter list; they
  451. Xuse the most-recently initialized or restarted RNG to produce generates.
  452. XMacros whose names do not contain a ``v'' have the vector and number of
  453. Xgenerates omitted from their parameter list; they produce and return a
  454. Xsingle generate.
  455. X
  456. XAll generating routines abort with a message to \code{stderr} if called
  457. Xwith an invalid \code{RNGdata} pointer.\\
  458. X
  459. X\noindent
  460. X{\bf Buffering\\}
  461. X
  462. XThe operation of the buffered routines and their interaction with the
  463. Xunbuffered routines requires some elaboration.  Each RNG maintains two
  464. Xseparate buffers: one for buffering bit values (the ``bit buffer''), and
  465. Xone for buffering all other values (the ``main buffer'').  The size of
  466. Xthe main buffer of an RNG is determined at the time of the RNG's
  467. Xinitialization, while the size of the bit buffer is currently fixed at
  468. X32 bits.
  469. X
  470. XConsider a freshly-initialized RNG with a main buffer size of 1000.  A
  471. Xrequest is made for a single generate of type \code{long}.  The RNG's
  472. Xbuffer gets filled with 1000 generates, and the first such generate is
  473. Xreturned to the user.  So the buffer now contains 999 generates.  If
  474. Xanother generate of type \code{long}, \code{double}, or \code{float}, is
  475. Xrequested, a generate will be pulled from the buffer and returned to the
  476. Xuser after being converted to the proper type.  If the user continues to
  477. Xrequest generates in this way and the main buffer becomes depleted, it
  478. Xwill be filled again with 1000 generates, and so on.
  479. X
  480. XThe unbuffered routines do not interfere with either of the RNG's
  481. Xbuffers.  Again, consider our RNG, with its buffer filled with 1000
  482. Xgenerates.  The user now makes a request for a single unbuffered
  483. Xgenerate.  The underlying RNG will then be called once, returning a
  484. Xsingle generate, leaving our buffer of 1000 generates untouched, and
  485. Xstill ready to be accessed by the buffered routines.  If, in a
  486. Xparticular application, it is necessary to always use consecutive
  487. Xgenerates from an RNG, then that RNG should always be called {\em
  488. Xeither} with buffered or unbuffered routines, but not with a combination
  489. Xof both.
  490. X
  491. XThe bit buffer of an RNG operates similarly to the main buffer, with one
  492. Xkey difference: the bit buffer is filled by sequentially retrieving
  493. Xgenerates from the main buffer.  Once again, consider our RNG, with its
  494. Xbuffer filled with 1000 generates, and with its bit buffer empty.  A
  495. Xsingle bit is then requested.  Thirty-two generates will be pulled from
  496. Xthe main buffer, transformed into thirty-two one-bit 0-1 values, then
  497. Xstored in the bit buffer.  (For users who are more concerned with speed
  498. Xthan accuracy, we also provide a ``fast'' bit-buffer call, in which a
  499. Xsingle 32-bit generate from the main buffer is transformed into
  500. Xthirty-two 0-1 variates.  See the descriptions of \code{bxrandom_f} and
  501. X\code{brandom_f}, below.)\\
  502. X
  503. X\pagebreak
  504. X\noindent
  505. X{\bf Unbuffered and buffered calling procedures\\}
  506. X
  507. X\begin{example}
  508. X    double dxrandomrv(rng, n, v)
  509. X    double drandomrv(rng, n, v)
  510. X    RNGdata *rng;
  511. X    long n;
  512. X    double v[];
  513. X
  514. X    float fxrandomrv(rng, n, v)
  515. X    float frandomrv(rng, n, v)
  516. X    RNGdata *rng;
  517. X    long n;
  518. X    float v[];
  519. X
  520. X    long lxrandomrv(rng, n, v)
  521. X    long lrandomrv(rng, n, v)
  522. X    RNGdata *rng;
  523. X    long n;
  524. X    long v[];
  525. X
  526. X    int bxrandomrv(rng, n, v)
  527. X    int brandomrv(rng, n, v)
  528. X    RNGdata *rng;
  529. X    long n;
  530. X    int v[];
  531. X
  532. X    int bxrandomrv_f(rng, n, v)
  533. X    int brandomrv_f(rng, n, v)
  534. X    RNGdata *rng;
  535. X    long n;
  536. X    double v[];\\
  537. X\end{example}
  538. X
  539. XThese routines fill the vector \code{v} with \code{n} generates from
  540. X\code{rng}, and return the first generate produced (i.e. \code{v[0]}).
  541. X
  542. XIf \code{rng} is a null pointer, then the most-recently initialized or
  543. Xrestarted RNG is used to produce generates.  If \code{n} is 0, then the
  544. X\code{v} parameter need not be provided, and a single generate is
  545. Xproduced and returned.
  546. X
  547. X\code{bxrandomrv} uses one generate from \code{rng} to generate each
  548. Xbit.  This routine is slower than \code{bxrandomrv_f}, but returns bits
  549. Xof higher quality.
  550. X
  551. X\begin{tex}
  552. X\code{bxrandomrv\_f} uses each generate from \code{rng} to produce 32
  553. Xbits.  Therefore, requests for bits in other than multiples of 32 will
  554. Xresult in bits from the stream being ``lost'' between calls.  The
  555. Xroutine returns -1 if it is called with an RNG whose range is not $2^{32}$.
  556. XThis routine is faster than \code{bxrandomrv}, but we do not recommend
  557. Xits use, since we know of no one who has rigorously tested such a bit
  558. Xstream.  We gain confidence in our slower \code{bxrandomrv} bit stream,
  559. Xin comparison, every time the underlying generator passes a test
  560. Xsensitive to correlations in the leading digit of its floating point
  561. Xoutput, or to the most significant bit of its fixed point output.
  562. X\end{tex}
  563. X
  564. X\code{brandomrv_f}, the buffered version of \code{bxrandomrv_f}, does not
  565. Xexhibit the same property of ``losing bits'' as does \code{bxrandomrv_f}, since
  566. Xbits which are not used in one call to \code{brandomrv_f} are stored in the bit
  567. Xbuffer and are available for use upon future calls.\\
  568. X
  569. X\begin{example}
  570. X    int flush_rng(rng)
  571. X    RNGdata *rng;\\
  572. X\end{example}
  573. X
  574. X    Flushes both of the RNG's buffers.\\
  575. X
  576. X\noindent
  577. X{\bf Procedure for generating integers in a restricted range\\}
  578. X\label{sec:mrandomrv}
  579. X
  580. X\begin{example}
  581. X    long mrandomrv(rng, m, n, v)
  582. X    RNGdata *rng;
  583. X    long m,n,v[];\\
  584. X\end{example}
  585. X
  586. X\begin{tex}
  587. X\code{mrandomrv} fills the vector \code{v} with \code{n} generates in
  588. Xthe range $0..\code{m}-1$ using \code{rng}, where $1 \leq \code{m} \leq
  589. X\code{range\_rng(rng)}$.  If $\code{range\_rng(rng)} < \code{m}$, the
  590. Xprogram aborts with an error.
  591. X\end{tex}
  592. X
  593. XThe algorithm used by \code{mrandomrv} to fill \code{v} is set by
  594. X\code{init_rng} or by \code{mralg_rng}.  (See Section \ref{sec:initkill}
  595. Xand Section \ref{sec:examandmodify}.)
  596. X
  597. X\begin{tex}
  598. XAlgorithm 0 is Thomborson's unbiased method, which produces unbiased
  599. Xlong integers in the range [0..\code{m}).  The algorithm discards any
  600. Xoutputs from \code{rng} which are larger than $r-(r \bmod \code{m})$, where
  601. Xr is equal to \code{range\_rng(rng)}.  At worst, this code will discard
  602. X(on long-term average) at most one value of r for every one that is
  603. Xuseful.  This worst case is only encountered for extremely large
  604. X\code{m}; for fixed and moderate \code{m}, this code will rarely discard a
  605. Xvalue, and thus will run essentially as fast as algorithm 1.  When the
  606. Xvalue of \code{m} changes on each call to \code{mrandom}, however, this
  607. Xcode is slower than algorithm 1, due to the necessity of recomputing
  608. X$r-(r \bmod \code{m})$.
  609. X
  610. XThe program aborts with an error message to \code{stderr} if \code{rng}
  611. Xis behaving so non-randomly that Algorithm 0 must make an excessive
  612. Xnumber of calls to \code{rng} in order to produce the requested number of
  613. Xgenerates.
  614. X
  615. XThe program aborts with an error to \code{stderr} if \code{mrandomrv} is
  616. Xasked to use Algorithm 0 with a value of \code{m} for which $\code{m} >
  617. X\code{range\_rng(rng)}$. \\
  618. X
  619. X
  620. XAlgorithm 1 is the standard \code{(long)(m*dxrandomr(rng))}.  This
  621. Xalgorithm may be biased: for large \code{m}, some results may be be more
  622. Xlikely than others.  The bias is $\frac{r \bmod \code{m}}{\code{m}}$, which is
  623. Xupper-bounded by 0.1\% if \code{m} is less than a million and the range
  624. Xr of \code{rng} is at least a billion.
  625. X
  626. XWe do not support, and indeed we actively discourage, generating
  627. Xrestricted-range integers with \code{lrandomr(rng)\%m}.  Many RNGs have
  628. Xpoor behavior under this transformation, most noticeably when \code{m}
  629. Xis a power of 2.  When \code{m} is not a power of 2, fixed-point
  630. Xdivision required by an ``\code{\%}'' operation is time-consuming on many
  631. Xworkstations.\\
  632. X\end{tex}
  633. X
  634. X\noindent
  635. X{\bf NOTES}
  636. X
  637. X\begin{tex}
  638. XThe \code{mrandomrv} procedure is capable of generating long integers in
  639. Xthe full range of any RNG for which $1 \leq \code{range\_rng(rng)} \leq
  640. X2^{32}$.  In order to accomplish this, with the parameter \code{m} a
  641. Xsigned long integer, the following mapping is used:
  642. X
  643. X\begin{tabbing}
  644. XRange(\code{mrandom(m)}) = \=    $0..\code{m}-1$ \hspace{.5in} \= if $1 \leq
  645. X\code{m} < 2^{31}$\\
  646. X\> $0.. 2^{32}-1$ \> if $\code{m}=0$\\
  647. X\> $0..(2^{31}-\code{m}-1)$ \> if $-2^{31} \leq \code{m} < 0$
  648. X\end{tabbing}
  649. X\end{tex}
  650. X
  651. XMacros are defined for easy calling of \code{mrandomrv} with various
  652. Xdefault parameters.  See Section \ref{sec:genproc} for the naming
  653. Xconventions followed by the macros.
  654. X
  655. X\subsubsection{Saving and restarting RNGs}
  656. X\label{sec:saverestart}
  657. X
  658. XRNGs may be saved to disk files in a human-readable ASCII format and
  659. Xlater restarted.  RNG buffers are not saved, and therefore all restarted
  660. XRNGs have empty buffers, and any data remaining in an RNG's buffer at
  661. Xthe time of its state-save will {\em not} be reconstructed.\\
  662. X
  663. X\begin{example}
  664. X    int save_rng(rng, filename)
  665. X    RNGdata *rng;
  666. X    char *filename;
  667. X
  668. X    RNGdata *restart_rng(filename)
  669. X    char *filename;\\
  670. X\end{example}
  671. X
  672. X\code{save_rng} saves \code{rng} to the ASCII file named \code{filename}.
  673. X
  674. X\code{restart_rng} restarts an RNG from a previously saved statefile.
  675. XThe restarted RNG will begin where the saved RNG ``left off.''  As with
  676. X\code{init_rng}, the \code{RNGdata} pointer used to store the restarted
  677. XRNG {\em must} be either a freshly declared pointer or a pointer to a
  678. Xfreshly killed RNG (see Section \ref{sec:initkill}).
  679. X
  680. XRNGs may store their state and seed vectors in any of a number of
  681. Xformats, and this is reflected in the format of the state file.  Figure
  682. X\ref{fig:samplestatefile1} shows a sample state file of an RNG using
  683. Xthe Knuth/Bentley lagged-Fibonnacci generator prand (see Appendix
  684. X\ref{app:installedrngs}), which stores its state and seeds as 32-bit
  685. X\code{long int}s.  Figure \ref{fig:samplestatefile2} shows a sample
  686. Xstate file of an RNG using 4.3bsd \code{nrand48}, which stores its state
  687. Xand seeds as 16-bit \code{int}s.
  688. X
  689. X\begin{figure}
  690. X\begin{example}
  691. XRNG statefile for algorithm 2, (Knuth/Bentley prand: lagged Fibbonacci)
  692. XBuffer size = 1024 bytes
  693. XInitial seed table =
  694. X   00000927
  695. XNumber of calls to underlying RNG after seeding = 0 billion + 2000
  696. XNext value in this pseudorandom sequence = 0b64d0ea
  697. XThis RNG returns every 1 generates
  698. XThis RNG uses mrandom algorithm 0
  699. XRNG state table =
  700. X   0911c27a 10641ca0 2ba00807 1aabed0a
  701. X   273ff367 1ab88564 2ae76a9e 2a7e6bc0
  702. X   35c7568e 201b6b04 3ad90695 303208b2
  703. X   1e718896 054c9886 00e8c93f 130a41cb
  704. X   11de97bf 0da54e15 2f4fcca0 0ebb1f70
  705. X   01c195c3 3283980e 37dee108 0893a89b
  706. X   326849b0 167bb45e 19cc9765 33d97b51
  707. X   36b425d1 35704e34 29a638ca 280a086f
  708. X   11dfa5d6 14dcbcc4 2610bdf4 02534109
  709. X   2817daf4 0bcf76ab 19b0a07d 0eebf7f6
  710. X   113c003e 31b996b0 12bab234 05eddb36
  711. X   1ed71381 377742a3 3878e079 2668c922
  712. X   22cc8033 22368c85 18e960ea 2002b06f
  713. X   22ff23e8 251187dc 340c3dcd 00000023
  714. X   00000004
  715. X\end{example}
  716. X\caption{A sample RNG state file for the Knuth/Bentley prand().}
  717. X\label{fig:samplestatefile1}
  718. X\end{figure}
  719. X
  720. X\begin{figure}
  721. X\begin{example}
  722. XRNG statefile for algorithm 4, (4.3bsd nrand48.c: 48-bit multiplicative)
  723. XBuffer size = 8192 bytes
  724. XInitial seed table =
  725. X   0096   b43f   0034   bf15
  726. X
  727. XNumber of calls to underlying RNG after seeding = 0 billion + 11000
  728. XNext value in this pseudorandom sequence = 04a3689e
  729. XThis RNG returns every 1 generates
  730. XThis RNG uses mrandom algorithm 0
  731. XRNG state table =
  732. X   07c5   8f2d   0000   a7d6
  733. X\end{example}
  734. X\caption{A sample RNG state table for {\tt nrand48}}
  735. X\label{fig:samplestatefile2}
  736. X\end{figure}
  737. X
  738. XA few examples of how to save and restart RNGs are displayed in Figure
  739. X\ref{fig:saverestart}.
  740. X
  741. X\begin{figure}
  742. X\begin{example}
  743. X/* Proper way to re-initialize an active RNG */
  744. Xmrandom(rng,10,n,v);
  745. Xkill_rng(rng);
  746. Xrng=restart_rng("mystatefile");
  747. X
  748. X/* Proper way to restart an inactive RNG */
  749. XRNGdata *rng;
  750. Xrng=restart_rng("mystatefile");
  751. X
  752. X/* Improper way to restart an active RNG */
  753. Xmrandom(rng,10,n,v);
  754. Xrng=restart_rng("mystatefile");
  755. X\end{example}
  756. X\caption{Examples of saving and restarting RNGs}
  757. X\label{fig:saverestart}
  758. X\end{figure}
  759. X
  760. X\subsubsection{Seeding}
  761. X\label{sec:seedproc}
  762. X
  763. XEach RNG is initially seeded during initialization by
  764. X\code{init_rng}.  An RNG may also be reseeded at any time after
  765. Xinitialization.\\
  766. X
  767. X\begin{example}
  768. X    void seed_rng(rng,seed)
  769. X    RNGdata *rng;
  770. X    long *seed;\\
  771. X\end{example}
  772. X
  773. X\code{seed_rng} seeds \code{rng} with the seed table pointed to by \code{seed}.
  774. XThe RNG's counter is reset to 0 and its buffers are flushed.
  775. X
  776. X\subsubsection{Checking RNG integrity}
  777. X    An RNG can be checked to see if it has been corrupted or is
  778. Xotherwise not in proper condition for use.\\
  779. X
  780. X\begin{example}
  781. X    int check_rng(rng);
  782. X    RNGdata *rng;\\
  783. X\end{example}
  784. X
  785. X\code{check_rng} checks the integrity of the RNG, in order to determine
  786. Xwhether it can be used by the other mrandom library routines.
  787. X
  788. X\subsubsection{Obtaining a human-readable description of the RNG}
  789. X
  790. X\begin{example}
  791. X    char *describe_rng(rng,rngid)
  792. X    RNGdata *rng;
  793. X    char rngid[RNGIDSTRLEN];\\
  794. X\end{example}
  795. X
  796. X\code{describe_rng} places a human-readable description of \code{rng} in
  797. Xthe string \code{rngid}.  The string has the following format:\\
  798. X
  799. X\begin{example}
  800. XRNG state identifier is (alg, mralg: seed1, seed2; count1,count2;
  801. Xbufsize, split)\\
  802. X\end{example}
  803. X
  804. Xwhere
  805. X
  806. X\begin{itemize}
  807. X\item \code{alg} is the number of the algorithm used by \code{rng} to generate
  808. Xpseudorandom numbers.  (See Appendix \ref{app:installedrngs}.)
  809. X\item \code{mralg} is the number of the algorithm used by
  810. X\code{mrandomrv} when called with \code{rng}.  (See Section
  811. X\ref{sec:mrandomrv}.)
  812. X\item \code{seed1} and \code{seed2} are the first and second entries in
  813. Xt\code{rng}'s seed table.  If \code{rng}'s seed table has more than two
  814. Xentries, only the first two are included in its description.  (See
  815. XSection \ref{sec:seedproc}.)
  816. X\item \code{count1} and \code{count2} represent \code{rng}'s counter.
  817. X(See Section \ref{sec:initkill}.)
  818. X\item \code{bufsize} is the number of entries in \code{rng}'s buffer.
  819. X(See Section \ref{sec:genproc}.)
  820. X\item \code{split} is \code{rng}'s current split value.  (See Section
  821. X\ref{sec:examandmodify}.)
  822. X\end{itemize}
  823. X
  824. X\code{describe_rng} exits with a message to \code{stderr} if called
  825. Xwith an invalid \code{RNGdata} pointer.
  826. X
  827. X\subsubsection{Procedures for examining and modifying RNG parameters}
  828. X\label{sec:examandmodify}
  829. X
  830. XProcedures are available for examining and modifying an RNG's parameters
  831. Xonce it has been initialized.\\
  832. X
  833. X\begin{example}
  834. X    int mralg_rng(rng, new_value)
  835. X    RNGdata *rng;
  836. X    long new_value;
  837. X
  838. X    int split_rng(rng, new_value)
  839. X    RNGdata *rng;
  840. X    long new_value;
  841. X
  842. X    double range_rng(rng)
  843. X    RNGdata *rng;\\
  844. X\end{example}
  845. X
  846. X\code{mralg_rng} sets \code{rng}'s mrandom algorithm number (See Section
  847. X\ref{sec:mrandomrv} for information on mrandom algorithm numbers).  It
  848. Xreturns 0 if \code{new_value} is an invalid value.
  849. X
  850. X\code{split_rng} sets the split value of \code{rng}.  It returns 0 if
  851. X\code{new_value} < 0.  An RNG's split value is set to \code{SPLIT_DEF}
  852. Xupon initialization.  \code{SPLIT_DEF} is \code{#define}d in
  853. X\file{mrandom.h}, and currently has a value of 0.
  854. X
  855. XThe function of the split value is to simulate one ``branch'' of a
  856. Xgenerator which has been ``split'' into two or more generators.  This is
  857. Xbest illustrated with an example.  Consider an (apparently non-random)
  858. XRNG which returns the raw sequence:
  859. X\begin{center}
  860. X0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...\\
  861. X\end{center}
  862. XThe split value indicates how many elements of the sequence to skip
  863. Xbetween generates.  For example, if our sample RNG were given a split
  864. Xvalue of 1 immediately after initialization, it would then return the following
  865. Xsequence:
  866. X\begin{center}
  867. X0 2 4 6 8 10 ...\\
  868. X\end{center}
  869. XA split value of 2 after initialization would produce:
  870. X\begin{center}
  871. X0 3 6 9 12 15 ...\\
  872. X\end{center}
  873. XAn RNG may be split at any time after its initialization.  So, for
  874. Xexample, our sample RNG might be initialized and then made to generate
  875. Xthe following values:
  876. X\begin{center}
  877. X0 1 2 3 4 5\\
  878. X\end{center}
  879. Xbefore being split with a split value of 3, producing the following
  880. Xgenerates:
  881. X\begin{center}
  882. X6 10 14 18 22 ...\\
  883. X\end{center}
  884. XSplitting can be used to create several ``leapfrogged'' RNGs from one
  885. XRNG, as shown in Figure \ref{fig:leapfrog}.
  886. X
  887. X\begin{figure}
  888. X\begin{example}
  889. XRNGdata *rngs[10];
  890. Xlong seed;
  891. Xint i;
  892. X
  893. Xseed=12345;
  894. Xfor (i=0; i<10; i++) \{
  895. X  /* RNG #i gets cycled i times */
  896. X  rng[i]=init_rng(2,0,&seed,i,0,1024);
  897. X  split(rng[i],9);
  898. X\}
  899. X\end{example}
  900. X\caption{Creating ``leapfrogged'' RNGs}
  901. X\label{fig:leapfrog}
  902. X\end{figure}
  903. X
  904. XThis operation may be useful in parallel codes, as in testing an RNG for
  905. Xlong-range correlations.  Unfortunately, our current implementation is
  906. Xinefficient for leapfrogging large numbers of RNGs.  A more efficient
  907. Xmethod may be included in a future version of mrandom.
  908. X
  909. XThe split value affects all of the pseudorandom number generating
  910. Xroutines (See Section \ref{sec:genproc}).
  911. X
  912. X\code{range_rng} returns the range of \code{rng}.\\
  913. X
  914. X\noindent
  915. X{\bf NOTES}
  916. X
  917. XThese procedures exit with a message to \code{stderr} if
  918. X\code{rng} does not point to a valid \code{RNGdata} structure.
  919. X
  920. X\section{Adding new RNGs to the Package}
  921. X
  922. XThis section is designed for the programmer who wishes to add new RNGs
  923. Xto the mrandom package.  The section first describes the routines which
  924. Xmust be provided by the programmer to serve as an interface between the
  925. XRNG and the mrandom package.  It then describes the RNG parameters which
  926. Xmust be defined in a header file for the RNG, and how these parameters
  927. Xare to be incorporated into the code for mrandom itself.  Finally, it
  928. Xdescribes how to remake the mrandom package after adding or modifying
  929. XRNGs.  Appendix \ref{app:installedrngs} contains descriptions of the ten
  930. XRNGs installed in the current version of the package.
  931. X
  932. X\subsection{Routines Provided by the Programmer}
  933. X\label{sec:progroutines}
  934. X\subsubsection{Introduction}
  935. X
  936. XThis section describes routines which must be provided by each RNG in
  937. Xthe package.
  938. X
  939. XThe routines provided by the programmer must manipulate an
  940. X\code{RNGdata} structure.  In order to facilitate this, two macros are
  941. Xavailable for accessing an RNG's state and seed vectors:
  942. X\begin{itemize}
  943. X\item \code{RNGstate} refers to the RNG's state vector.
  944. X\item \code{RNGseed} refers to the RNG's seed vector.
  945. X\end{itemize}
  946. XThese vectors are one-dimensional C arrays, e.g.
  947. X\code{RNGstate[0]} is the first element in the RNG's state vector,
  948. X\code{RNGstate[1]} is the second element, etc.  In order to make use of
  949. Xthese macros, the name of the \code{RNGdata} pointer in your routines'
  950. Xparameter lists {\em must} be \code{rng}, as shown in the examples in
  951. Xthis section.
  952. X
  953. XThe names used as examples in this section begin with ``\code{myrng}'';
  954. Xhowever, there are no restrictions on naming of routines provided by an
  955. XRNG.  However, for ease of readability and consistency, we suggest that
  956. Xthe naming conventions used in this section be followed.
  957. X
  958. XThe routines described in this section should be included in a single
  959. X\code{.c} file.  A template for such a source file, called
  960. X\file{newrng.c}, is included in the distribution and displayed in Figure
  961. X\ref{fig:newrng.c}.
  962. X
  963. XRemember that all RNG state information must be included in the
  964. X\code{RNGstate} field of the \code{RNGdata} structure.  In particular,
  965. Xdo {\em not} use global or static variables to hold RNG state
  966. Xinformation.  Doing so will make it impossible to run several
  967. Xinstantiations of your RNG simulataneously.
  968. X
  969. X\begin{figure}
  970. X\begin{example}
  971. X/* newrng.c */
  972. X/* RNG source file template */
  973. X/* Robert Plotkin
  974. X/* 5/3/93 */
  975. X
  976. X#include "newrng.h"
  977. X
  978. X/* Generating procedure */
  979. X/* Only one of the following two procedures should be */
  980. X/* defined, depending on the kind of value that */
  981. X/* your RNG returns */
  982. X
  983. Xlong newrng_lgen(rng)
  984. XRNGdata *rng;
  985. X\{
  986. X/* Your generating procedure goes here */
  987. X\}
  988. X
  989. Xdouble newrng_dgen(rng)
  990. XRNGdata *rng;
  991. X\{
  992. X/* Your generating procedure goes here */
  993. X\}
  994. X
  995. X/* Seeding procedure */
  996. Xvoid newrng_seed(rng,seed)
  997. XRNGdata *rng;
  998. Xlong *seed;
  999. X\{
  1000. X/* Your seeding procedure goes here */
  1001. X\}
  1002. X
  1003. X/* Checking procedure */
  1004. Xint newrng_check(rng)
  1005. XRNGdata *rng;
  1006. X\{
  1007. X/* Your checking procedure goes here */
  1008. X\}
  1009. X\end{example}
  1010. X\caption{RNG source file template}
  1011. X\label{fig:newrng.c}
  1012. X\end{figure}
  1013. X
  1014. X\subsubsection{Seeding Routine}
  1015. X
  1016. X\begin{example}
  1017. X    void myrng_seed(rng, seed)
  1018. X    RNGdata *rng;
  1019. X    long *seed;\\
  1020. X\end{example}
  1021. X
  1022. XThis procedure is used for seeding the RNG.  The interpretation of the
  1023. X\code{seed} parameter is left entirely to the programmer.  It may, for
  1024. Xexample, point to a single integer or to an array of 5,000 integers.
  1025. XOne of the RNGs currently installed in the package interprets the
  1026. X\code{seed} parameter as pointing to three 16-bit integers.  Obviously,
  1027. XRNGs which are capable of being seeded with a variable number of seeds
  1028. Xneed to be passed a seed pointer which contains adequate information
  1029. Xabout the number of seeds to which it points.
  1030. X
  1031. XAlthough the seeding procedure is passed an entire \code{RNGdata}
  1032. Xstructure as a parameter, it should only manipulate the \code{RNGstate}
  1033. Xfield of that structure.  (See Appendix \ref{app:rngdata} for information
  1034. Xon the \code{RNGdata} structure.)  Many RNG seeding procedures will simply
  1035. Xcopy the seed parameter into \code{RNGstate}, as shown in Figure
  1036. X\ref{fig:sampleseed}.
  1037. X
  1038. X\begin{figure}
  1039. X\begin{example}
  1040. X    void myrng_seed(rng, seed)
  1041. X    RNGdata *rng;
  1042. X    long *seed;
  1043. X    \{
  1044. X    RNGstate[0]=seed[0];
  1045. X    RNGstate[1]=seed[1]; /* This RNG uses two long seeds */
  1046. X    \}
  1047. X\end{example}
  1048. X\caption{A sample seeding procedure}
  1049. X\label{fig:sampleseed}
  1050. X\end{figure}
  1051. X
  1052. XOther seeding procedures may fill \code{RNGstate} with the results
  1053. Xof some complicated function performed on the initial seed table.
  1054. X
  1055. X\subsubsection{Pseudorandom Number Generating Procedure}
  1056. X
  1057. X\begin{example}
  1058. Xlong myrng_lgen(rng)
  1059. XRNGdata *rng;
  1060. X
  1061. Xdouble myrng_dgen(rng)
  1062. XRNGdata *rng;\\
  1063. X\end{example}
  1064. X
  1065. XThe programmer must provide {\em one} procedure, matching one of the two
  1066. Xprototypes given above, which returns a
  1067. Xsingle generate from the RNG.  The routine may return either:
  1068. X\begin{itemize}
  1069. X\item a double precision floating point number in the range [0,1), or
  1070. X\item a long (32-bit) integer in the range 0..\code{range_rng(rng)}-1
  1071. X\end{itemize}
  1072. X
  1073. X\noindent
  1074. XIt is pointless for the programmer to provide procedures of both types.
  1075. XIf this is done, only one of them will be accessible by any user code,
  1076. Xdepending on the value given to \code{RNGreturns}, \code{RNGdgen}, and
  1077. X\code{RNGlgen} (see Section \ref{sec:rngheaders}).
  1078. X
  1079. XAlthough the generating procedure is passed an entire \code{RNGdata}
  1080. Xstructure, it should only manipulate the \code{RNGstate} field of the
  1081. Xstructure.  A sample generating procedure is displayed in Figure
  1082. X\ref{fig:samplegen}.
  1083. X
  1084. X\begin{figure}
  1085. X\begin{example}
  1086. Xlong myrng_lgen(rng)
  1087. XRNGdata *rng;
  1088. X\{
  1089. XRNGstate[0]*=12345+6789;
  1090. Xreturn(RNGstate[0]);
  1091. X\}
  1092. X\end{example}
  1093. X\caption{A sample generating procedure}
  1094. X\label{fig:samplegen}
  1095. X\end{figure}
  1096. X
  1097. X\subsubsection{RNG Checking Procedure}
  1098. X
  1099. X\begin{example}
  1100. Xint myrng_check(rng)
  1101. XRNGdata *rng;\\
  1102. X\end{example}
  1103. X
  1104. XThe programmer must provide a procedure to check the
  1105. Xintegrity of the \code{RNGdata} structure.  The procedure returns a value
  1106. Xof 1 if the RNG is fit for use, and returns 0 otherwise.  The
  1107. Xcoding of the procedure is entirely RNG-specific, and may be
  1108. Xextremely simple or extremely complicated, depending on the nature
  1109. Xof the RNG and the extent of integrity desired.  On one extreme is
  1110. Xthe procedure which always declares success, and on the other
  1111. Xextreme is the perfect (and slow) procedure which creates a new
  1112. XRNG, seeds it with the seeds of the RNG to be checked, cycles it
  1113. Xthrough the number of generates which were produced by the RNG to
  1114. Xbe checked, and compares the state tables of the two RNGs.
  1115. XClearly, the procedure should not modify the RNG in any way.  When
  1116. Xwriting a checking procedure it might be useful to examine those
  1117. Xincluded in the existing package.
  1118. X
  1119. X\subsection{RNG Header Files}
  1120. X\label{sec:rngheaders}
  1121. X
  1122. XFor each RNG included in the package, there must be a
  1123. Xcorresponding header file.  The header file contains information
  1124. Xabout the RNG which is used by the mrandom library routines.
  1125. XThis section describes the information contained in RNG header
  1126. Xfiles, and describes how to use such header files to incorporate
  1127. Xnew RNGs into the mrandom package.  A template for such a header file,
  1128. Xcalled \file{newrng.h}, is included in the distribution and displayed in
  1129. XFigure \ref{fig:newrng.h}.
  1130. X
  1131. X\begin{figure}
  1132. X\begin{example}
  1133. X/* newrng.h */
  1134. X/* RNG header file template */
  1135. X/* Robert Plotkin
  1136. X/* 5/3/93 */
  1137. X
  1138. X#include "mrandom.h"
  1139. X
  1140. X/* Information for mrandom */
  1141. X#define RNGstatesize_n    
  1142. X#define RNGseedsize_2
  1143. X#define RNGrange_2
  1144. X#define RNGname_2
  1145. X#define RNGreturns_2
  1146. X#define RNGstatetype_2
  1147. X#define RNGdgen_2
  1148. X#define RNGlgen_2
  1149. X#define RNGseed_2
  1150. X#define RNGcheck_2
  1151. X
  1152. X/* mrandom interface routines */
  1153. Xlong newrng_gen(/* RNGdata * */);
  1154. Xvoid newrng_seed(/* RNGdata *, long * */);
  1155. Xint newrng_check(/* RNGdata * */);
  1156. X\end{example}
  1157. X\caption{RNG header file template}
  1158. X\label{fig:newrng.h}
  1159. X\end{figure}
  1160. X
  1161. X\subsubsection{Information in Header Files}
  1162. X\label{sec:headers}
  1163. X
  1164. XEach header file should begin with the following directives:\\
  1165. X
  1166. X\begin{example}
  1167. X#ifndef MRANDOM
  1168. X#include "mrandom.h"
  1169. X#endif\\
  1170. X\end{example}
  1171. X
  1172. X\pagebreak
  1173. X\noindent
  1174. X{\bf Definition of RNG parameters\\}
  1175. X
  1176. XThe next set of lines in the file should contain \code{#define}
  1177. Xstatements which assign values to the RNG's parameters.  The names used
  1178. Xin the \code{#define} statements must contain the RNG's number.  There
  1179. Xare currently ten RNGs included in the package, labeled 0 through 9.
  1180. XThe next RNG included in the package should be labeled number 10, and so
  1181. Xon.  The ``\code{n}'' in each parameter name in the following list
  1182. Xshould be interpreted as the RNG's number.
  1183. X
  1184. X\begin{description}
  1185. X\item[\code{RNGname_n}] A string constant containing the name of the RNG,
  1186. Xterminated with a newline character.
  1187. X\item[\code{RNGstatesize_n}] The number of entries in the RNG's state
  1188. Xtable.  Each entry is a (32-bit) \code{long}.  If the RNG is
  1189. Xcapable of using state tables of varying sizes, \code{RNGstatesize_n}
  1190. Xshould be defined as the maximum possible size.
  1191. X\item[\code{RNGseedsize_n}] The number of entries in the RNG's seed table.
  1192. XEach entry is a (32-bit) \code{long}.  If the RNG is capable of
  1193. Xusing seed tables of varying sizes, \code{RNGseedsize_n} should be
  1194. Xdefined as the maximum possible size.
  1195. X\item[\code{RNGrange_n}]
  1196. X\begin{tex} The range of the RNG, expressed as a double
  1197. Xprecision floating point number.  The range of the RNG is one more
  1198. Xthan the maximum value the RNG is capable of generating.  For RNGs
  1199. Xwhich produce double precision generates with a precision of p
  1200. X(i.e. in the range $[0,(\code{RNGrange}-1.0)/(1<<p))$, \code{RNGrange}
  1201. Xshould be defined as $2^{p}$.  For example, an RNG which produces 8-byte
  1202. XIEEE floating-point generates using single-precision IEEE arithmetic
  1203. X(24-bit mantissas) has a range of 16777216.0.
  1204. X\end{tex}
  1205. X\item[\code{RNGreturns_n}] A number signifying the type of the generate
  1206. Xreturned by the RNG.  An RNG can return a value of one of two types:
  1207. X\begin{itemize}
  1208. X\item a \code{long} in the range 0..\code{RNGrange}-1
  1209. X\item a \code{double} in the range [0,1)
  1210. X\end{itemize}
  1211. XRNGs which return values of type \code{long} and \code{double} return
  1212. Xtypes \code{RET_LONG} and \code{RET_DOUBLE}, respectively, as defined in
  1213. X\file{mrandom.h}.
  1214. X\item[\code{RNGstatetype}] A number signifying the interpretation of the
  1215. Xvalues stored in the RNG's state and seed vectors.  This value is used
  1216. Xby the routines that read and write the ASCII state files, thereby
  1217. Xallowing portability of state files across machines with different byte
  1218. Xorderings (see Section \ref{sec:saverestart}).  The following values
  1219. Xare currently supported:
  1220. X\begin{tex}
  1221. X\begin{center}
  1222. X\begin{tabular}{l l}
  1223. XValue & Type\\
  1224. X\hline\\
  1225. X\code{STATE\_CHAR} & 8-bit character\\
  1226. X\code{STATE\_INT} & 16-bit integer\\
  1227. X\code{STATE\_LONG} & 32-bit long integer\\
  1228. X\end{tabular}
  1229. X\end{center}
  1230. X\end{tex}
  1231. XThe values of \code{STATE\_FLOAT} (IEEE-standard 32-bit float) and
  1232. X\code{STATE\_DOUBLE} (IEEE-standard 64-bit float) are not currently
  1233. Xsupported and are reserved for future use.
  1234. X
  1235. X\item[\code{RNGdgen_n} and \code{RNGlgen_n}] The label of the procedure to be
  1236. Xused for generating pseudorandom numbers.  If the RNG returns
  1237. X\code{double}s, then \code{RNG_dgen} should be defined as the label of
  1238. Xthe RNG generating procedure, and \code{RNG_lgen} should be defined as
  1239. X0.  If the RNG returns \code{long}s, then \code{RNG_lgen} should be
  1240. Xdefined as the label of the RNG generating procedure, and
  1241. X\code{RNG_dgen} should be defined as 0.
  1242. X\item[\code{RNGseed_n}] The label of the procedure to be used for seeding
  1243. Xthe RNG.
  1244. X\item[\code{RNGcheck_n}] The label of the procedure to be used for
  1245. Xchecking the integrity of the RNG.
  1246. X\end{description}
  1247. X
  1248. X\noindent
  1249. X{\bf Procedure prototypes\\}
  1250. X
  1251. XFinally, the header file must contain function prototypes for
  1252. Xthe three procedures provided by the RNG, so that the procedures
  1253. Xcan be accessed by the main mrandom code.  For example:\\
  1254. X
  1255. X\begin{example}
  1256. Xlong myrng_gen();
  1257. Xvoid myrng_seed();
  1258. Xint myrng_check();
  1259. X\end{example}
  1260. X
  1261. X\subsection{Modifying the mrandom code}
  1262. XOnly a few lines of \file{mrandom.h} and \file{mrandom.c} need to be
  1263. Xmodified when adding a new RNG to the package.
  1264. X
  1265. X\begin{itemize}
  1266. X\item The number of RNGs currently installed in the package is defined as
  1267. X\code{NUM_RNGS} in \file{mrandom.h}.  The current value is 10.  This
  1268. Xvalue should be incremented when a new RNG is added to the package.
  1269. X\item The header file for the new RNG needs to be \code{#include}d in
  1270. X\file{mrandom.c}.  The \code{#include} directive should be included in the
  1271. Xsection marked by the comment ``\code{Header files for RNGs currently included in package.}''
  1272. X\item Several additions need to be made in \file{mrandom.c} in the
  1273. Xsection marked by the comment ``\code{Arrays to hold information about
  1274. XRNGs.}''  This section of the code declares and initializes several
  1275. Xarrays which hold information about the RNGs included in the
  1276. Xpackage.
  1277. X\pagebreak
  1278. XWhen installing a new RNG, the appropriate \code{#define}d values need
  1279. Xto be inserted at the end of each initialization list.  For example, the
  1280. Xdeclaration of \code{RNGname\_a} currently reads:\\
  1281. X
  1282. X\begin{example}
  1283. Xchar RNGname\_a[NUM\_RNGS][RNGIDSTRLEN]=\{RNGname\_0, RNGname\_1,
  1284. X      RNGname\_2, RNGname\_3, RNGname\_4, RNGname\_5, RNGname\_6,
  1285. X      RNGname\_7, RNGname\_8, RNGname\_9\};\\
  1286. X\end{example}
  1287. XAfter adding a new RNG to the package, this declaration would read:\\
  1288. X\begin{example}
  1289. Xchar RNGname\_a[NUM_RNGS][RNGIDSTRLEN]=\{RNGname\_0, RNGname\_1,
  1290. X      RNGname\_2, RNGname\_3, RNGname\_4, RNGname\_5, RNGname\_6,
  1291. X      RNGname\_7, RNGname\_8, RNGname\_9,
  1292. X      /* RNG #10 added -> */ RNGname\_10\};\\
  1293. X\end{example}
  1294. XThe arrays \code{statesize\_a}, \code{seedsize\_a}, \code{range\_a},
  1295. X\code{returns\_a}, \code{statetype\_a}, \code{seed\_a}, \code{dgen\_a},
  1296. X\code{lgen\_a}, and \code{check\_a} need to be similarly modified.
  1297. X\end{itemize}
  1298. X
  1299. X\subsection{Remaking the mrandom Package}
  1300. X    Once you have added an RNG to the package as described in the
  1301. Xprevious sections, you will need to remake the mrandom package.  To do
  1302. Xthis:
  1303. X\begin{itemize}
  1304. X\item Make sure that all of the files for the mrandom package,
  1305. Xinclude the source and header files for your new RNG, are in the
  1306. Xsame directory.
  1307. X
  1308. X\item Include the names of your header, source, and object files in
  1309. X\file{makefile} on the lines labeled \code{INCS}, \code{SRCS}, and
  1310. X\code{OBJS}, respectively, as show in Figure \ref{fig:addmake}.
  1311. X
  1312. X\begin{figure}
  1313. X\begin{example}
  1314. XINCS = mrandom.h bentley.h pcrand.h ran0.h ran1.h ran2.h ultra.h xsq.h myrng.h
  1315. XSRCS = mrtest.c mrandom.c bentley.c pcrand.c ran0.c ran1.c ran2.c ultra.c xsq.c myrng.c
  1316. XOBJS = mrandom.o bentley.o pcrand.o ran0.o ran1.o ran2.o ultra.o xsq.o myrng.o
  1317. X\end{example}
  1318. X\caption{Addition of {\tt myrng} to {\tt makefile}}
  1319. X\label{fig:addmake}
  1320. X\end{figure}
  1321. X
  1322. X\item Follow the instructions for making the mrandom package, as
  1323. Xdescribed in Section \ref{sec:install}.
  1324. X\end{itemize}
  1325. XOnce the package has been remade it will be ready for use, with your new
  1326. XRNG, by other programs.
  1327. X
  1328. X\pagebreak
  1329. X\appendix
  1330. X\section{The RNGdata Structure}
  1331. X\label{app:rngdata}
  1332. X\subsection{Introduction}
  1333. XThis section describes the representation in C of the \code{RNGdata}
  1334. Xstructure which is used by the mrandom package to represent RNGs.  This
  1335. Xstructure need never be manipulated by the programmer, except as
  1336. Xdescribed in Section \ref{sec:progroutines}.  This section, therefore,
  1337. Xis intended for those who are interested in learning a little more about
  1338. Xthe inner workings of the mrandom package.
  1339. X
  1340. XIn order to generate random numbers, the user must first declare a
  1341. Xpointer to an \code{RNGdata} structure, and use \code{init_rng} to
  1342. Xallocate space for the RNG and perform various initialization functions.
  1343. XThe user uses the RNG entirely through calls provided by the interface
  1344. Xdescribed in Section \ref{sec:library}; i.e. the user should not
  1345. Xdirectly manipulate the \code{RNGdata} structure.
  1346. X
  1347. X\subsection{Inside the Structure}
  1348. X    The definition of the \code{RNGdata} structure is displayed in
  1349. XFigure \ref{fig:rngdata}.
  1350. X
  1351. X\begin{figure}
  1352. X\begin{example}
  1353. X\begin{tabbing}
  1354. Xstruct \= rngdata \{ \+ \\
  1355. Xlong rngalg;\\
  1356. Xlong mrandom_alg;\\
  1357. Xlong *rngstate;\\
  1358. Xlong *rngseed;\\
  1359. Xlong rngcount1;\\
  1360. Xlong rngcount2;\\
  1361. Xstruct \{ \= \+ \\
  1362. Xlong size;\\
  1363. Xlong nleft;\\
  1364. Xlong nbleft;\\
  1365. Xdouble *dbuf,*dbufp;\\
  1366. Xlong *lbuf,*lbufp;\\
  1367. Xint *bbuf,*bbufp;\\
  1368. X\} buffer; \- \\
  1369. Xlong rngnextval;\\
  1370. Xlong rngsplit;\\
  1371. Xchar rngname[];\\
  1372. Xlong rngstatesize;\\
  1373. Xlong rngseedsize;\\
  1374. Xlong rngrangem1;\\
  1375. Xdouble rngrange;\\
  1376. Xsigned int rngreturns; \- \\
  1377. X\}; \\
  1378. Xtypedef struct rngdata RNGdata;
  1379. X\end{tabbing}
  1380. X\end{example}
  1381. X\caption{The {\tt RNGdata} structure}
  1382. X\label{fig:rngdata}
  1383. X\end{figure}
  1384. X
  1385. XDescriptions of its fields are as follows:
  1386. X
  1387. X\begin{description}
  1388. X\item[\code{rngalg}] A number identifying the algorithm to be used by
  1389. Xthe RNG to produce pseudorandom generates.  Algorithms in the package
  1390. Xare numbered sequentially starting with 0; currently there are 10
  1391. Xalgorithms installed, numbered 0 through 9.  A table of RNGs which are
  1392. Xcurrently installed in the mrandom package, with their corresponding
  1393. Xalgorithm numbers, is in Appendix \ref{app:installedrngs}.
  1394. X\item[\code{mrandom_alg}] The algorithm use by \code{mrandomrv} when
  1395. Xcalled with this RNG.  See Section \ref{sec:mrandomrv} for more on
  1396. X\code{mrandomrv}.
  1397. X\item[\code{rngstate}] A pointer to the RNG's state vector, used to
  1398. Xstore the current state of the RNG.  See Sections \ref{sec:saverestart},
  1399. X\ref{sec:progroutines}, and \ref{sec:headers} for more information on RNG
  1400. Xstate vectors.
  1401. X\item[\code{rngseed}] A pointer to the RNG's seed vector.  See Section
  1402. X\ref{sec:seedproc} for more information on RNG seed vectors.
  1403. X\item[\code{rngcount1}, \code{rngcount2}] These two values represent the
  1404. Xnumber of generates the RNG has produced since initialization, according
  1405. Xto the formula:\\
  1406. X\begin{example}
  1407. Xrngcount1+rngcount2*BILLION\\
  1408. X\end{example}
  1409. Xwhere BILLION is defined in \file{mrandom.h}.  Please note that the
  1410. Xvalue represented by \code{rngcount1} and \code{rngcount2} is the {\it
  1411. Xtotal} number of generates produced by the RNG since initialization,
  1412. Xincluding those discarded due to splitting of the RNG.  (See Section
  1413. X\ref{sec:examandmodify} for more information about splitting RNGs.)
  1414. X\item[\code{rngnextval}] The next value to be output from the RNG.  This
  1415. Xvalue is used internally by the mrandom library and is not guaranteed to
  1416. Xbe accurate.
  1417. X\item[\code{rngsplit}] Every (\code{split}+1)-th generate of the
  1418. Xunderlying RNG will be returned by the RNG calling procedures.
  1419. X\code{rngsplit} is set to \code{DEF_SPLIT} upon initialization of the
  1420. XRNG, as defined in \file{mrandom.h}.  See Section \ref{sec:examandmodify}
  1421. Xfor more information about splitting RNGs.
  1422. X\item[\code{buffer}] This structure contains information about the RNG's
  1423. Xbuffer and its bit buffer.  (See Section \ref{sec:genproc} for more
  1424. Xinformation on RNG buffers.)  It contains several fields:
  1425. X\begin{description}
  1426. X\item[\code{size}] The number of entries in the RNG's buffer.
  1427. X\item[\code{nleft}] The number of values left in the RNG's buffer.
  1428. X\item[\code{nbleft}] The number of values left in the RNG's bit buffer.
  1429. X\item[\code{dbuf}, \code{dbufp}] A pointer to the first entry in the double
  1430. Xbuffer, and a pointer to the next entry to be retrieved from the
  1431. Xdouble buffer.
  1432. X\item[\code{lbuf}, \code{lbufp}] Same for the long buffer.
  1433. X\item[\code{bbuf}, \code{bbufp}] Same for the bit buffer.
  1434. X\end{description}
  1435. X\end{description}
  1436. X
  1437. XThe remaining values in the RNGdata structure are derived
  1438. Xfrom the RNG's header file upon initialization.  For more information on
  1439. Xthe values of these fields, see Section \ref{sec:headers}.
  1440. X
  1441. X\section{RNGs Currently Installed in the Package}
  1442. X\label{app:installedrngs}
  1443. XThere are currently ten RNGs installed in the mrandom package.  This
  1444. Xappendix provides brief descriptions of each of them.  References are
  1445. Xprovided for those who are interested in finding out about the RNGs in
  1446. Xmore detail.
  1447. X
  1448. X\begin{description}
  1449. X
  1450. X\item[RNG algorithm 0: A trivial RNG] A trivial RNG is included in the
  1451. Xpackage, primarily for testing purposes.  The generates it produces are
  1452. Xnot ``random'' in virtually any sense of the word; it simply produces
  1453. Xgenerates from an arithmetical progression determined by its initial
  1454. Xseeds.  For example, if it is seeded with 5 and 7, respectively, it will
  1455. Xproduce the sequence 5, 12, 19, 26, etc.
  1456. X
  1457. XThis RNG takes two \code{long}s as seeds.  It returns generates
  1458. Xof type \code{long}.
  1459. X
  1460. X\item[RNG algorithm 1: 4.3bsd random]
  1461. X\begin{tex}
  1462. XThis is UNIX 4.3bsd \code{random}.  It is a 31-bit nonlinear additive
  1463. Xfeedback generator with a range of $2^{31}$ and a period of
  1464. Xapproximately $16*2^{31}-1$.  It is nominally able to save and restore
  1465. Xstate, but its state-saving code is buggy.  Therefore, when using
  1466. X\code{random} with the mrandom package, no more than one RNG should use
  1467. X\code{random} at a time.
  1468. X\end{tex}
  1469. X
  1470. XThis RNG takes a single \code{long} as a seed.  It returns generates of
  1471. Xtype \code{long}.
  1472. X
  1473. X\item[RNG algorithm 2: the Knuth/Bentley prand]  This
  1474. Xlagged-Fibonacci RNG was introduced by Jon Bentley in his ``Software
  1475. XExploratorium'' column in {\em Unix Review}, Vol. 10, No. 6, June 1992,
  1476. Xand is based on one first presented in Donald E. Knuth's {\em The Art of
  1477. XComputer Programming}, Vol. 2, Addison-Wesley, Reading, Mass., 1981.
  1478. XIt has a range of 1,000,000,000.
  1479. X
  1480. XThis RNG takes a single \code{long} as a seed.  It returns generates of
  1481. Xtype \code{long}.
  1482. X
  1483. X\item[RNG algorithm 3: The Portable Combined RNG]  This combined
  1484. Xprime multiplicative congruential RNG was developed based on algorithms
  1485. Xand selections of prime numbers presented in ``Efficient and Portable
  1486. XCombined Random Number Generators,'' Pierre L'Ecuyer, {\em
  1487. XCommunications of the ACM}, Vol. 10, No. 6, June 1992, and ``Random
  1488. XNumber Generators: Good Ones are Hard to Find,'' Stephen Park and Keith
  1489. XMiller, {\em Communications of the ACM}, Vol. 31, No. 10, October 1992.
  1490. XIt has a range of 2147483561.
  1491. X
  1492. XThis RNG takes two \code{long}s as seeds.  It returns generates of type
  1493. X\code{long}.
  1494. X
  1495. X\item[RNG algorithm 4: 4.3bsd nrand48]
  1496. X\begin{tex}
  1497. XThis is UNIX 4.3bsd \code{nrand48}.  It produces generates using a
  1498. Xlinear congruential algorithm and 48-bit integer arithmetic.  It has a
  1499. Xrange of $2^{31}$.
  1500. X\end{tex}
  1501. X
  1502. XThis RNG takes three \code{unsigned short}s as seeds.  They are passed
  1503. Xto the seeding procedure as two \code{long}s, and are interpreted in the
  1504. Xfollowing way:
  1505. X\begin{itemize}
  1506. X\item The 16 least significant bits of the second \code{long} is the
  1507. Xfirst seed.
  1508. X\item The 16 least significant bits of the first \code{long} is the
  1509. Xsecond seed.
  1510. X\item The 16 most significant bits of the first \code{long} is the third
  1511. Xseed.
  1512. X\item The 16 most significant bits of the second \code{long} is
  1513. Xignored.
  1514. X\end{itemize}
  1515. XThis RNG returns generates of type \code{long}.
  1516. X
  1517. X\item[RNG algorithm 5: 4.3bsd rand]
  1518. X\begin{tex}
  1519. XThis is UNIX 4.3bsd \code{rand}.  It uses a multiplicative congruential
  1520. Xalgorithm.  It has a period of $2^{32}$ and a range of $2^{31}$.
  1521. X\end{tex}
  1522. X
  1523. XThis RNG takes a single \code{long} as a seed.  It returns generates of
  1524. Xtype \code{long}.
  1525. X
  1526. X\item[RNG algorithm 6, 7, and 8: Press and Teukolsky's ran0, ran1, and
  1527. Xran2]
  1528. X\begin{tex}
  1529. XThese three multiplicative congruential RNGs are adapted from those
  1530. Xpresented in ``Portable Random Number Generators,'' William H.  Press
  1531. Xand Saul A. Teukolsky, {\em Computers in Physics}, Vol. 6, No. 5,
  1532. XSep/Oct 1992.  They all have a period of $2^{31}-2$ and a range of
  1533. X$2^{31}-1$.
  1534. X\end{tex}
  1535. XThese RNGs take a single \code{long} as a seed.  They return generates
  1536. Xof type \code{double}.
  1537. X\item[RNG algorithm 9: Marsaglia's Ultra RNG]
  1538. X\begin{tex}
  1539. X
  1540. XWe obtained the source code for this generator by anonymous ftp from
  1541. X\code{nic.funit.fi} (take the file \code{fsultra.zip} from the directory
  1542. X\code{/pub/msdos/science/math/fsultra}).  A note in the \file{readme}
  1543. Xfile says: ``To obtain permission to incorporate this program into any
  1544. Xcommercial product, please contact the authors at the e-mail address
  1545. Xgiven above [afir@stat.fsu.edu or geo@stat.fsu.edu] or at Department of
  1546. XStatistics and Supercomputer Computations Research Institute, Florida
  1547. XState University, Tallahassee, FL 32306.''  This RNG is one of those
  1548. Xoriginally presented in ``A New Class of Random Number Generators,''
  1549. XGeorge Marsaglia and Arif Zaman, {\em The Annals of Applied
  1550. XProbability}, Vol. 1, No. 3, 1991.  It is a ``subtract-with-borrow''
  1551. Xgenerator with a range of $2^{32}$ and a staggering period of
  1552. X$10^{354}$.
  1553. X\end{tex}
  1554. X
  1555. XThis RNG takes two \code{unsigned long}s as seeds.  It returns generates
  1556. Xof type \code{double}.
  1557. X\end{description}
  1558. X
  1559. X\end{document}
  1560. END_OF_FILE
  1561. if test 56912 -ne `wc -c <'doc/mrandom.tex'`; then
  1562.     echo shar: \"'doc/mrandom.tex'\" unpacked with wrong size!
  1563. fi
  1564. # end of 'doc/mrandom.tex'
  1565. fi
  1566. echo shar: End of archive 4 \(of 6\).
  1567. cp /dev/null ark4isdone
  1568. MISSING=""
  1569. for I in 1 2 3 4 5 6 ; do
  1570.     if test ! -f ark${I}isdone ; then
  1571.     MISSING="${MISSING} ${I}"
  1572.     fi
  1573. done
  1574. if test "${MISSING}" = "" ; then
  1575.     echo You have unpacked all 6 archives.
  1576.     rm -f ark[1-9]isdone
  1577. else
  1578.     echo You still need to unpack the following archives:
  1579.     echo "        " ${MISSING}
  1580. fi
  1581. ##  End of shell archive.
  1582. exit 0
  1583.