<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <!--Converted with LaTeX2HTML 2008 (1.71) original version by: Nikos Drakos, CBLU, University of Leeds * revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan * with significant contributions from: Jens Lippmann, Marek Rouchal, Martin Wilck and others --> <HTML> <HEAD> <TITLE>Random Numbers</TITLE> <META NAME="description" CONTENT="Random Numbers"> <META NAME="keywords" CONTENT="manual"> <META NAME="resource-type" CONTENT="document"> <META NAME="distribution" CONTENT="global"> <META NAME="Generator" CONTENT="LaTeX2HTML v2008"> <META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css"> <LINK REL="STYLESHEET" HREF="manual.css"> <LINK REL="next" HREF="node14.html"> <LINK REL="previous" HREF="node12.html"> <LINK REL="up" HREF="manual.html"> <LINK REL="next" HREF="node14.html"> </HEAD> <BODY > <!--Navigation Panel--> <A NAME="tex2html274" HREF="node14.html"> <IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A> <A NAME="tex2html270" HREF="manual.html"> <IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A> <A NAME="tex2html264" HREF="node12.html"> <IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A> <A NAME="tex2html272" HREF="node1.html"> <IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A> <BR> <B> Next:</B> <A NAME="tex2html275" HREF="node14.html">Interpolation</A> <B> Up:</B> <A NAME="tex2html271" HREF="manual.html">A Discrete EVent system</A> <B> Previous:</B> <A NAME="tex2html265" HREF="node12.html">Alternate types for time</A> <B> <A NAME="tex2html273" HREF="node1.html">Contents</A></B> <BR> <BR> <!--End of Navigation Panel--> <H1><A NAME="SECTION001300000000000000000"> Random Numbers</A> </H1> Adevs has two classes that work together to generate random numbers. These classes are the <B>random_seq</B> class and the <B>rv</B> class. The <B>random_seq</B> class provides uniformly distributed random numbers to the <B>rv</B> class. The <B>rv</B> class transforms these uniform random numbers into a variety of random number distributions. <P> The <B>random_seq</B> class is the interface for a random number generator. Its derived classes produce uniformly distributed pseudo-random numbers. The underlying random number stream is accessed with two methods. The method <B><I>next_long</I></B> returns a random number as an unsigned long. The method <B><I>next_dlb</I></B> refines the <B><I>next_long</I></B> method by reducing that random number to a double precision number in the interval <IMG WIDTH="36" HEIGHT="32" ALIGN="MIDDLE" BORDER="0" SRC="img59.png" ALT="$ [0,1]$"> . The random number sequence is initialized with the <B><I>set_seed</I></B> method, and the entire random number generator can be copied with the <B><I>copy</I></B> method. To summarize, the <B>random_seq</B> class has four virtual methods <PRE> void set_seed(unsigned long seed) double next_dbl() random_seq* copy() const unsigned long next_long() </PRE> that must be implemented by any derived class. <P> Adevs comes with two implementations of the <B>random_seq</B> class: the <B>crand</B> class and the <B>mtrand</B> class. The <B>crand</B> class uses the rand function from the standard C library to implement the required methods. Its implementation is trivial. I've listed it below as an example of how to implement the <B>random_seq</B> interface. <PRE> class crand: public random_seq { public: /// Create a generator with the default seed crand(){} /// Create a generator with the given seed crand(unsigned long seed) { srand (seed); } /// Set the seed for the random number generator void set_seed(unsigned long seed) { srand (seed); } /// Get the next double uniformly distributed in [0, 1] double next_dbl() { return (double)rand()/(double)RAND_MAX; } /// Copy the random number generator unsigned long next_long() { return (unsigned long)rand(); } random_seq* copy() const { return new crand (); } /// Destructor ~crand(){} }; </PRE> <P> The <B>mtrand</B> class implements the Mersenne Twister random number generator<A NAME="tex2html30" HREF="footnode.html#foot3019"><SUP>12.1</SUP></A>. This code is based on their open source implementation of the Mersenne Twister. Aside from its potential advantages as a random number generator, the <B>mtrand</B> class differs from the <B>crand</B> class by its ability to make deep copies. Every instance of the <B>mtrand</B> class has its own random number stream. <P> The <B>rv</B> class uses the uniform random numbers provided by a <B>random_seq</B> object to produce several different random number distributions: triangular, uniform, normal, exponential, lognormal, Poisson, Weibull, binomial, and many others. Every instance of the <B>rv</B> class is created with a <B>random_seq</B>. The default is an <B>mtrand</B> object, but any type of <B>random_seq</B> object can be passed to the <B>rv</B> constructor. The different random distributions are sampled by calling the appropriate method: <B><I>triangular</I></B> for a triangular distribution, <B><I>exponential</I></B> for an exponential distribution, <B><I>poisson</I></B> for a Poisson distribution, etc. Because Adevs is open source software, if a new distribution is needed then you can add a method that implements it to the <B>rv</B> class (and, I hope, contribute the expansion to the Adevs project). <HR> <!--Navigation Panel--> <A NAME="tex2html274" HREF="node14.html"> <IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A> <A NAME="tex2html270" HREF="manual.html"> <IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A> <A NAME="tex2html264" HREF="node12.html"> <IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A> <A NAME="tex2html272" HREF="node1.html"> <IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A> <BR> <B> Next:</B> <A NAME="tex2html275" HREF="node14.html">Interpolation</A> <B> Up:</B> <A NAME="tex2html271" HREF="manual.html">A Discrete EVent system</A> <B> Previous:</B> <A NAME="tex2html265" HREF="node12.html">Alternate types for time</A> <B> <A NAME="tex2html273" HREF="node1.html">Contents</A></B> <!--End of Navigation Panel--> <ADDRESS> 2013-06-18 </ADDRESS> </BODY> </HTML>