Main Page Class Hierarchy Alphabetical List Compound List File List Compound Members File Members Related Pages

# XFuRandom.cpp

Go to the documentation of this file.

    00001 /*! \file
    00002  * X-Forge Util <br>
    00003  * Copyright 2000-2003 Fathammer Ltd
    00004  *
    00005  * \brief Mersenne Twister random generator
    00006  *
    00007  * Information about the algorithm can be found at:
    00008  * http://www.math.keio.ac.jp/~matumoto/emt.html
    00009  *
    00010  * $Id: XFuRandom.cpp,v 1.8 2003/08/12 13:34:08 lars Exp $
    00011  * $Date: 2003/08/12 13:34:08 $
    00012  * $Revision: 1.8 $
    00013  */
    00014
    00015 #include <xfcore/XFcCore.h>
    00016 #include <xfutil/XFuRandom.h>
    00017
    00018
    00019 enum XFURANDOM_MERSENNE_PARAMETERS
    00020 {
    00021     XFURMP_N = 351,
    00022     XFURMP_M = 175,
    00023     XFURMP_R = 19,
    00024     XFURMP_LOMASK = 0x0007FFFF,
    00025     XFURMP_HIMASK = 0xFFFFE000,
    00026     XFURMP_A = 0xEABD75F5,
    00027     XFURMP_U = 11,
    00028     XFURMP_S = 7,
    00029     XFURMP_T = 15,
    00030     XFURMP_L = 17,
    00031     XFURMP_B = 0x655E5280,
    00032     XFURMP_C = 0xFFD58000
    00033 };
    00034
    00035
    00036 XFuRandom::~XFuRandom()
    00037 {
    00038     delete[] mTable;
    00039     mTable = NULL;
    00040 }
    00041
    00042
    00043 XFuRandom::XFuRandom() : mTable(NULL)
    00044 {
    00045 }
    00046
    00047
    00048 XFuRandom * XFuRandom::create()
    00049 {
    00050     return create((UINT32)XFcCore::getTick());
    00051 }
    00052
    00053
    00054 XFuRandom * XFuRandom::create(UINT32 aSeed)
    00055 {
    00056     XFuRandom *res = new XFuRandom;
    00057
    00058     if (res == NULL)
    00059         return NULL;
    00060
    00061     res->mTable = new UINT32[XFURMP_N];
    00062     if (res->mTable == NULL)
    00063     {
    00064         delete res;
    00065         return NULL;
    00066     }
    00067
    00068     res->seed(aSeed);
    00069
    00070     return res;
    00071 }
    00072
    00073
    00074 UINT32 XFuRandom::next()
    00075 {
    00076     UINT32 value;
    00077
    00078     if (mTableIndex >= XFURMP_N)
    00079     {
    00080         UINT32 i, j;
    00081         for (i = 0, j = XFURMP_M; i < XFURMP_N - 1; ++i)
    00082         {
    00083             value = (mTable[i] & XFURMP_HIMASK) | (mTable[i + 1] & XFURMP_LOMASK);
    00084             mTable[i] = mTable[j] ^ (value >> 1) ^ (-(INT32)(value & 1) & XFURMP_A);
    00085             ++j;
    00086             if (j >= XFURMP_N)
    00087                 j = 0;
    00088         }
    00089
    00090         value = (mTable[XFURMP_N - 1] & XFURMP_HIMASK) | (mTable[0] & XFURMP_LOMASK);
    00091         mTable[XFURMP_N - 1] = mTable[XFURMP_M - 1] ^ (value >> 1) ^ (-(INT32)(value & 1) & XFURMP_A);
    00092         mTableIndex = 0;
    00093     }
    00094
    00095     value = mTable[mTableIndex];
    00096     ++mTableIndex;
    00097
    00098     value ^= (value >> XFURMP_U);
    00099     value ^= (value << XFURMP_S) & XFURMP_B;
    00100     value ^= (value << XFURMP_T) & XFURMP_C;
    00101     value ^= (value >> XFURMP_L);
    00102
    00103     return value;
    00104 }
    00105
    00106
    00107 void XFuRandom::seed(UINT32 aSeed)
    00108 {
    00109     UINT32 s = aSeed;
    00110     for (mTableIndex = 0; mTableIndex < XFURMP_N; ++mTableIndex)
    00111     {
    00112         s = (s * 29943829) - 1;
    00113         mTable[mTableIndex] = s;
    00114     }
    00115 }
    00116

|  | X-Forge Documentation Confidential Copyright © 2002-2003 Fathammer |  | Documentation generated with doxygen by Dimitri van Heesch