Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

MZRan13.h

Go to the documentation of this file.
00001 // -*- mode: C++; c-file-style: "stroustrup"; c-basic-offset: 4; indent-tabs-mode: nil; -*-
00002 ////////////////////////////////////////////////////////////////////
00003 //
00004 // Filename : MZRan13.h
00005 //
00006 // This file is a part of the UPPAAL toolkit.
00007 // Copyright (c) 1995 - 2003, Uppsala University and Aalborg University.
00008 // All right reserved.
00009 //
00010 // $Id: MZRan13.h,v 1.2 2004/09/08 09:02:57 johanb Exp $
00011 //
00012 ///////////////////////////////////////////////////////////////////
00013 
00014 #ifndef INCLUDE_BASE_MZRAN13
00015 #define INCLUDE_BASE_MZRAN13
00016 
00017 #include <base/inttypes.h>
00018 
00019 namespace base 
00020 {
00021 
00022 /* 32-bit (pseudo) random number generator.
00023  * Random number generator due to Marsaglia and Zaman.
00024  * Generates 32-bit numbers with a period of about 2^125.
00025  *
00026  * @section Reference
00027  * Marsaglia, G. and A. Zaman. 1994. Some portable very-long period random
00028  * number generators. Computers in Physics.  8(1): 117-121. 
00029  */
00030     class MZRan13 
00031     {
00032     public:
00033         /* Constructor
00034          * @param seed: The initial random seed.
00035          */
00036         MZRan13(uint32_t seed);
00037 
00038         /* Generate random number
00039          * @return the next pseudorandom number.
00040          */
00041         uint32_t operator()();
00042     private:
00043         uint32_t x, y, z, n, c;
00044     };
00045 
00046     inline MZRan13::MZRan13(uint32_t s)
00047         : x(s ^ 521288629), y(s ^ 362436069)
00048         , z(s ^ 16163801), n(s ^ 1131199209)
00049     {
00050         c = (y > z ? 1 : 0);
00051     }
00052 
00053     inline uint32_t MZRan13::operator()() 
00054     {
00055         uint32_t s;
00056         if (y > x + c) { 
00057             s = y - (x + c); 
00058             c = 0;
00059         } else {
00060             s = (y - (x + c) - 18) & 0xffffffff; 
00061             c = 1;
00062         }
00063         x = y;
00064         y = z;   
00065 
00066         return ((z = s) + (n = 69069 * n + 1013904243)) & 0xffffffff;
00067     }
00068 }
00069 
00070 #endif /* INCLUDE_BASE_MZRAN13 */

Generated on Fri Jun 30 00:02:30 2006 for Module base by  doxygen 1.4.2