Logo
~Sockets~
~Examples~
~Contact~


RandomNumber.cpp

Go to the documentation of this file.
00001 
00005 /*
00006 This program is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU General Public License
00008 as published by the Free Software Foundation; either version 2
00009 of the License, or (at your option) any later version.
00010 
00011 This program is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GN6U General Public License for more details.
00015 
00016 You should have received a copy of the GNU General Public License
00017 along with this program; if not, write to the Free Software
00018 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 */
00020 #include "RandomNumber.h"
00021 #include <limits>
00022 #include <time.h>
00023 
00024 #ifdef SOCKETS_NAMESPACE
00025 namespace SOCKETS_NAMESPACE {
00026 #endif
00027 
00028 const unsigned long int RandomNumber::X_SEED_DEFAULT = 123456789UL;
00029 const unsigned long int RandomNumber::Y_SEED_DEFAULT = 362436069UL;
00030 const unsigned long int RandomNumber::Z_SEED_DEFAULT = 521288629UL;
00031 const unsigned long int RandomNumber::W_SEED_DEFAULT = 88675123UL;
00032 
00033 
00034 RandomNumber::RandomNumber(bool time_shuffle)
00035 :mXSeed(time_shuffle ? (unsigned long)time(NULL) ^ X_SEED_DEFAULT : X_SEED_DEFAULT)
00036 ,mYSeed(time_shuffle ? (unsigned long)time(NULL) ^ Y_SEED_DEFAULT : Y_SEED_DEFAULT)
00037 ,mZSeed(time_shuffle ? (unsigned long)time(NULL) ^ Z_SEED_DEFAULT : Z_SEED_DEFAULT)
00038 ,mWSeed(time_shuffle ? (unsigned long)time(NULL) ^ W_SEED_DEFAULT : W_SEED_DEFAULT)
00039 {
00040   reset();
00041 }
00042 
00043 RandomNumber::RandomNumber(
00044   unsigned long int x_seed,
00045   unsigned long int y_seed,
00046   unsigned long int z_seed,
00047   unsigned long int w_seed)
00048 :mXSeed(x_seed)
00049 ,mYSeed(y_seed)
00050 ,mZSeed(z_seed)
00051 ,mWSeed(w_seed)
00052 {
00053   reset();
00054 }
00055 
00056 RandomNumber::~RandomNumber()
00057 {
00058 }
00059 
00060 void RandomNumber::reset()
00061 {
00062   mX = mXSeed;
00063   mY = mYSeed;
00064   mZ = mZSeed;
00065   mW = mWSeed;
00066 }
00067 
00068 RandomNumber::operator unsigned long int() const
00069 {
00070   return(mW);
00071 }
00072 
00073 unsigned long int RandomNumber::next()
00074 {
00075   register unsigned long int t = (mX ^ (mX<<11));
00076 
00077   mX = mY;
00078 
00079   mY = mZ;
00080 
00081   mZ = mW;
00082 
00083   return(mW = (mW ^ (mW>>19)) ^ (t ^ (t>>8)));
00084 }
00085 
00086 unsigned long int RandomNumber::skip(unsigned long int s)
00087 {
00088   for(register unsigned long int i = 0 ; i < s ; ++i)
00089   {
00090     (void)next();
00091   }
00092 
00093   return(mW);
00094 }
00095 
00096 void RandomNumber::getSeed(
00097   unsigned long int& x_seed,
00098   unsigned long int& y_seed,
00099   unsigned long int& z_seed,
00100   unsigned long int& w_seed)
00101 {
00102   x_seed = mXSeed;
00103   y_seed = mYSeed;
00104   z_seed = mZSeed;
00105   w_seed = mWSeed;
00106 }
00107 
00108 unsigned long int RandomNumber::max_random()
00109 {
00110   return(std::numeric_limits<unsigned long int>::max());
00111 }
00112 
00113 #ifdef SOCKETS_NAMESPACE
00114 } // namespace SOCKETS_NAMESPACE {
00115 #endif
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4