
% BB Aug 30 2007. Extracted Marsaglia random number part, added congruential.
% BB Sep 15 2004. Singapore.

\documentclass[20pt,landscape,footrule]{foils}
\usepackage[dvips]{graphicx}
\usepackage[pdftex]{geometry}
\geometry{headsep=3ex,hscale=0.9}
\usepackage[pdftex]{color} % for the colored block
\date{\color{blue} FSU MCMC Course ISC5228, August 28, 2007}
\usepackage[ignore]{pause}
%\usepackage{pause}
\usepackage{background}
%\usepackage{pp4slide}
%\MyLogo{AV HEP,CSIT}
\LogoOff
\begin{document}
\def\r{\color{red}}
\def\b{\color{blue}}
\def\s{\color{black}}

\title{\r Random Number Generator of STMC}
\author{\color{blue} Bernd Berg}
\maketitle
\definecolor{bgblue}{rgb}{0.04,0.39,0.53}
\definecolor{pictcol}{rgb}{0.04,0.29,0.44}
\vpagecolor{bgblue}

\foilhead{\r Random Numbers and Fortran Code} \b \vskip -20.0pt
% \noindent
According to Marsaglia and collaborators a list of desirable properties 
for random number generators is:

\begin{description}
\item{(i)} {\it Randomness}. The generator should pass stringent tests
for randomness.

\item{(ii)} {\it Long period}. 

\item{(iii)} {\it Computational efficiency}. 

\item{(iv)} {\it Repeatability}. Initial conditions (seed values) 
completely determine the resulting sequence of random variables.

\item{(v)} {\it Portability}. Identical sequences of random variables may 
be produced on a wide variety of computers (for given seed values).

\item{(vi)} {\it Homogeneity}. All subsets of bits of the numbers are
random.
\end{description}

Physicists have added a number of their applications as new tests. 
In particular the exact solution of the $2d$ Ising model is used.

Frequently used is the {\r congruential random number generator}. It
is based on the recursion
\begin{equation} \nonumber \s
  I_n = (a\,I_{n-1} + b)\, {\rm mod}(m)
\end{equation} 
where {\s $I_n,\, a,\, b$ and~$m$} are integers. Uniformly distributed
random numbers are then defined by the real numbers
\begin{equation} \nonumber \s
  x_n = I_n/m\ .
\end{equation} 
Theorems (see the book by Knuth) state that good choices of the integer
constants {\s $a,\, b$ and $m$} exists, so that the series of random
numbers becomes a permutation
\begin{equation} \nonumber \s
  \pi_0,\  \pi_1,\ \dots\ \pi_{m-2},\ \pi_{m-1}
\end{equation} 
of $\s 0,\  1,\ \dots\ {m-2},\ {m-1}.$

In STMC the {\r random number generator by Marsaglia} and collaborators
is provided. It has a period $2^{144}$ and fulfills also the other 
desirable properties well. It relies on a combination of two generators:

\begin{description}
\item $x_n$ from a lagged Fibonacci series
$I_n= I_{n-r}-I_{n-s}\ {\rm mod}\,2^{24}$, $r=97$, $s=33$.
% with a period of $(2^{24}-1)\times 2^{94} \approx 2^{110}$.

\item $y_n$ from the arithmetic series 
$I-k,\, I-2k,\, I-3k,\,\dots\,,\ {\rm mod}\,[2^{24}-3]$.
% where $2^{24}-3=16777213$ is a prime number and $k=7654321$. 
\end{description}

For most applications this generator is a good compromise. 
Our Fortran code which implements Marsaglia random numbers consists 
of three subroutines: 
%
\begin{description}
\item {\tt\r rmaset.f} to set the initial state of the random number 
      generator.
\item {\tt\r ranmar.f} which provides one random number per call.
\item {\tt\r rmasave.f} to save the final state of the generator. 
\end{description}
%
% In addition, {\tt rmafun.f} is a function version of {\tt ranmar.f} and 
% calls to these two routines are freely interchangeable. Related is also 
% the subroutine {\tt rmacau.f}, which generates Cauchy random numbers
% with $\alpha=1$. 

\noindent
The subroutine {\tt\r rmaset.f} continues a saved state or 
initializes the generator to independent sequences of 
random numbers defined by distinct pairs of seeds:
\begin{equation} \label{seeds}
-1801 \le {\tt iseed1} \le 29527 ~~{\rm and}~~
-9373 \le {\tt iseed2} \le 20708 \ .
\end{equation}
This property makes the generator quite useful for parallel processing. 
% The use of the subroutines {\tt rmaset.f}, {\tt ranmar.f} and 
% {\tt rmasave.f} is illustrated by the program \index{mar.f}{\tt mar.f}. 
% Table~\ref{tab_ranmar} illustrates a start and a
% continuation run of the generator. 
%
\begin{table}[tbp]
\centering
\caption{ Illustration of a start and a continuations run of the
Marsaglia random number generator using the program {\tt mar.f}
with the default seeds ({\tt a0102\_02}). }
% without a data file {\tt ranmar.d} present in the default run directory.
% The second {\tt (CONTINUATION)} run is performed with {\tt ranmar.d} 
% as obtained from the first run.}
\begin{verbatim}
   
      RANMAR INITIALIZED.               MARSAGLIA CONTINUATION.
      idat, xr = 1  0.116391063         idat, xr = 1  0.495856345
      idat, xr = 2  0.96484679          idat, xr = 2  0.577386141
      idat, xr = 3  0.882970393         idat, xr = 3  0.942340136
      idat, xr = 4  0.420486867         idat, xr = 4  0.243162394
      extra xr =    0.495856345         extra xr =    0.550126791
\end{verbatim}
\label{tab_ranmar}
\end{table}

\newpage
\r How to get and run the FORTRAN code?

\begin{figure} \begin{center}
\includegraphics[height=0.4\textheight]{figures/fort.pdf}
\caption{\small The Fortran routines are provided and prepared to 
run in the a tree structure of folders depicted in this figure. This 
tree of directories unfolds from the downloaded file. } 
\end{center} \end{figure}

\noindent\b To {\bf download} the Fortran code book visit the book
website and follow the instructions given there. 

\newpage \b
The code is provided in the directories {\r {\tt ForLib}, {\tt ForProg} 
and {\tt ForProc}.} {\tt ForLib} contains a library of functions and 
subroutines which is closed in the sense that no reference to 
non-standard functions or subroutines outside the library is ever 
made. Fortran programs are contained in the folder {\tt ForProg} and 
procedures for interactive use in {\tt ForProc}. 

{\bf Assignment: Marsaglia random numbers.} See coursework website.
Understand how to re-start the random number generator as well as how 
to perform different starts when the {\r continuation data file 
{\tt ranmar.d}} does not exist.
 
Note: To compile properly, main programs have to be located {\r two 
levels down from a root directory {\tt STMC}}.

\begin{center}
{\bf\r The hyperstructure of program dependencies introduced between 
the levels of the STMC directory tree should be kept intact! }
\end{center}
\begin{center}
(Unless you really know better.)
\end{center}

\end{document}

