\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#310: Add a New Pseudo-Random Number Generator}
\date{January 07, 2008}
\author{Arjen Markus}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#310}&\textbf{Add a New Pseudo-Random Number Generator}\\\hline
Author&

Arjen Markus \tipmail{arjen.markus}{wldelft.nl}
\\
Created&Monday, \(\text{7}^{\text{th}}\) January 2008\\
Type&Project\\
Tcl Version&\(8.6\)\\
State&Rejected\\
Vote&Done\\
Version&\tipversion$Revision: 1.3 $\\
Keywords&expr\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP proposes to add a new \textbf{expr} function: a random number generator with a longer sequence than the one currently in the core.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

The \textbf{expr} command has used a straightforward implementation of a pseudo-random number generator via the rand() function for many years. This has a number of desirable properties, but better ones (e.g. with a longer sequence) have been known for a long time. This TIP proposes a new function as an alternative PRNG.

PRNGs with different properties can be important for Monte Carlo simulations and other algorithms that require large amounts of random numbers without having to worry about the sequence length.

\section{Proposal}

Introduce a new function, \textit{randM()}, as an alternative PRNG to the \textbf{expr} command. This function is based on work by George Marsaglia and implementations in many different languages are available on the Internet. There will be a corresponding seed function \textit{srandM(x)}.

The new function is not a replacement of the original rand() function.

\section{Implementation}

With the new tcl::mathfunc mechanism (\cite{tip232}) it should be simple to add this function to the core.

An example implementation in Tcl is given below:
\begingroup\small\begin{verbatim}
 # marsaglia.tcl --
 #     Implementation of a PRNG according to George Marsaglia
 #
 namespace eval ::PRNG {
     variable mod [expr {wide(256)*wide(256)*wide(256)*wide(256)-5}]
     variable fac [expr {wide(256)*wide(32)}]
     variable x1 [expr {wide($mod*rand())}]
     variable x2 [expr {wide($mod*rand())}]
     variable x3 [expr {wide($mod*rand())}]

     puts $mod
 }

 proc ::PRNG::marsaglia {} {
     variable mod
     variable fac
     variable x1
     variable x2
     variable x3

     set xn [expr {($fac*($x3+$x2+$x1))%$mod}]

     set x1 $x2
     set x2 $x3
     set x3 $xn

     return [expr {$xn/double($mod)}]
 }
\end{verbatim}
\endgroup

\section{Copyright}

This document is placed in the public domain

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\begin{thebibliography}{TIP \#1}
\addcontentsline{toc}{section}{References}
\bibitem[TIP \#232]{tip232}
Arjen Markus,
Kevin Kenny,
\emph{Creating New Math Functions for the 'expr' Command},
on-line at \url{http://purl.org/tcl/tip/232.html}
\end{thebibliography}
\end{document}
