%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% ------------------------------------------------------------------------

% Generating Pseudo-random Numbers ***************************************

% ------------------------------------------------------------------------

% Submitted:      Dr. Eric Suess,Statistics 6501,Winter 2002

% Submitted Date: January 1st, 2002

% Accepted Date:  January 9th, 2002

% ------------------------------------------------------------------------

% This is a first draft of the handout "Generating Pseudo-random Numbers"

% in LaTeX.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\documentclass[12]{report}



\usepackage{latexsym}

\usepackage[centertags]{amsmath}

\usepackage{amsfonts}

\usepackage{newlfont}

%\usepackage{graphicx,fullpage,subfigure,pstricks}



\setlength{\oddsidemargin}{-0.4cm}



\setlength{\evensidemargin}{0cm}



\setlength{\topmargin}{-0.75cm}



\setlength{\textheight}{8.9in}



\setlength{\textwidth}{6.5in}



\renewcommand{\baselinestretch}{1}



\setlength{\parskip}{3mm}



\setlength{\parindent}{0mm} % indented paragraphs just don't



%\pagestyle{plain}



\begin{document}

\pagestyle{myheadings}

\markright{Statistics 6501, Winter 2002}



\large

\begin{center}

\textbf{Generating Pseudo-random Numbers}

\end{center}



\normalsize

\textbf{Linear Congruential Pseudo-random Number Generators}



Consider the function



\begin{equation}                            % single equation environment

g(x) = (Cx+D) \ \text{mod} \ M \nonumber    % small space \ space

\end{equation}



where $C$, $D$ and $M$ are constants.



Starting with an initial value $x_0$, we generate a sequence of

numbers, $x_0,\ x_1,\ x_2,\ x_3, \ldots$ by letting



\begin{equation}

x_{n+1} = g(x_n) \nonumber

\end{equation}



EXAMPLE: Let $M = 8,\ C = 5,\ D = 7,\ x_0 = 4$.  Then



\begin{equation}

g(x) = (5x + 7) \ \text{mod} \ 8 \nonumber

\end{equation}



Using this we obtain



\begin{eqnarray}                             % multi-line equation environment

x_1 &=& [(5)(4) + 7] \ \text{mod}  \ 8 = 3 \nonumber \\

x_2 &=& [(5)(3) + 7] \ \text{mod}  \ 8 = 6 \nonumber \\

x_3 &=& [(5)(6) + 7] \ \text{mod}  \ 8 = 5 \nonumber \\ \nonumber

\end{eqnarray}



Continuing in this way we find $x_4 = 0,\ x_5 = 7,\ x_6 = 2,\ x_7

= 1,\ x_8 = 4$.  At this point the sequence starts over again and

repeats the same 8 values over and over.



One thing to note about this example is that each of the values in

$\{0,\ldots,7\}$ occurs before the sequence begins repeating.  To

guarantee this, the values of $M,\ C$ and $D$

must be carefully chosen.\\



A number theory result guarantees that with the conditions listed

below, all the numbers in $\{0,\ldots,(M-1)\}$ will occur before

the sequence repeats.



\begin{enumerate}

  \item[(i)] $D$ and $M$ are relatively prime

  \item[(ii)] $C-1$ is divisible by every prime factor of

  \textit{M}

  \item[(iii)] If $M$ is divisible by 4 then so is $C-1$

\end{enumerate}



Since we would like a long sequence of random numbers we should

choose a very large value for $M$.  Also, we would like our number

generator to produce values between 0 and 1 (not between 0 and

$M-1$), so we will return the values $x_1/M,\ x_2/M,\ x_3/M,

\ldots$. We call such a number generator a Uniform(0,1) random

number generator.  We will see that all the random behavior we

would like to represent in a computer program can be derived from

a Uniform(0,1) random number generator.



\newpage        % starts new page



%\vspace{1in}   $ adds vertival space



The Pascal code below implements the method described above.  Note

that the variable \texttt{Seed} is global and must be initialized

at the beginning of the program execution.



\begin{verbatim}

var Seed : double



function Random : double;



const M = 1048576.0;

      C = 889925.0;

      D = 489459.0;



begin

Seed := C * Seed + D;

Seed := Seed - trunc(Seed / M) * M;

Random := Seed / M;

end;

\end{verbatim}



Equivalent code in \texttt{C} is displayed below.  Note that the

\texttt{fmod} function in \texttt{<math.h>} and that you will need

to use the \texttt{-lm} directive when compiling your code to link

the math library.



\begin{verbatim}

#define M 1048576.0

#define C 889925.0

#define D 489459.0



double Seed;



double Random (void)

{

  Seed = fmod(C * Seed + D, M);

  return (Seed / M);

}

\end{verbatim}



\large

\textbf{ For more information on random number generation see:}

\normalsize



\begin{itemize}

  \item Knuth, Donald, \textit{The Art of Computer Programming}.

  \item \textit{Numerical Recipes} available at most bookstores.

\end{itemize}



\end{document}

% ------------------------------------------------------------------------

