# random_func.cpp

Go to the documentation of this file.

    00001 /* $Id: random_func.cpp 12050 2008-02-03 16:21:19Z rubidium $ */
    00002
    00005 #include "../stdafx.h"
    00006 #include "random_func.hpp"
    00007 #include "bitmath_func.hpp"
    00008
    00009 Randomizer _random, _interactive_random;
    00010
    00011 uint32 Randomizer::Next()
    00012 {
    00013   const uint32 s = this->state[0];
    00014   const uint32 t = this->state[1];
    00015
    00016   this->state[0] = s + ROR(t ^ 0x1234567F, 7) + 1;
    00017   return this->state[1] = ROR(s, 3) - 1;
    00018 }
    00019
    00020 uint32 Randomizer::Next(uint16 max)
    00021 {
    00022   return GB(this->Next(), 0, 16) * max >> 16;
    00023 }
    00024
    00025 void Randomizer::SetSeed(uint32 seed)
    00026 {
    00027   this->state[0] = seed;
    00028   this->state[1] = seed;
    00029 }
    00030
    00031 #ifdef MERSENNE_TWISTER
    00032 // Source code for Mersenne Twister.
    00033 // A Random number generator with much higher quality random numbers.
    00034
    00035 #define N              (624)                 // length of _mt_state vector
    00036 #define M              (397)                 // a period parameter
    00037 #define K              (0x9908B0DFU)         // a magic constant
    00038 #define hiBit(u)       ((u) & 0x80000000U)   // mask all but highest   bit of u
    00039 #define loBit(u)       ((u) & 0x00000001U)   // mask all but lowest    bit of u
    00040 #define loBits(u)      ((u) & 0x7FFFFFFFU)   // mask     the highest   bit of u
    00041 #define mixBits(u, v)  (hiBit(u)|loBits(v))  // move hi bit of u to hi bit of v
    00042
    00043 static uint32 _mt_state[N+1];     // _mt_state vector + 1 extra to not violate ANSI C
    00044 static uint32 *_mt_next;          // _mt_next random value is computed from here
    00045 static int    _mt_left = -1;      // can *_mt_next++ this many times before reloading
    00046
    00047 void SetRandomSeed(register uint32 seed)
    00048 {
    00049   register uint32 *s = _mt_state;
    00050   _mt_left = 0;
    00051
    00052   seed |= 1U;
    00053   seed &= 0xFFFFFFFFU;
    00054
    00055   *s = seed;
    00056
    00057   for (register uint i = N; i != 0; i--) {
    00058     seed *= 69069U;
    00059     *s++;
    00060     *s = seed & 0xFFFFFFFFU;
    00061   }
    00062 }
    00063
    00064 static uint32 ReloadRandom()
    00065 {
    00066   if (_mt_left < -1) SetRandomSeed(4357U);
    00067
    00068   _mt_left = N - 1;
    00069   _mt_next = _mt_state + 1;
    00070
    00071   register uint32 *p0 = _mt_state;
    00072   register uint32 *p2 = _mt_state + 2;
    00073   register uint32 *pM = _mt_state + M;
    00074
    00075   register uint32 s0 = _mt_state[0];
    00076   register uint32 s1 = _mt_state[1];
    00077
    00078   register uint i = 0;
    00079
    00080   for (i = (N - M + 1); i != 0; i--) {
    00081     s0 = s1;
    00082     s1 = *p2;
    00083     *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U);
    00084     *p0++;
    00085     *p2++;
    00086     *pM++;
    00087   }
    00088
    00089   pM = _mt_state;
    00090
    00091   for (i = M; i != 0; i--) {
    00092     s0 = s1;
    00093     s1 = *p2;
    00094     *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U);
    00095     *p0++;
    00096     *p2++;
    00097     *pM++;
    00098   }
    00099
    00100   s1 = _mt_state[0];
    00101   *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U);
    00102
    00103   s1 ^= (s1 >> 11);
    00104   s1 ^= (s1 <<  7) & 0x9D2C5680U;
    00105   s1 ^= (s1 << 15) & 0xEFC60000U;
    00106   s1 ^= (s1 >> 18);
    00107   return s1;
    00108 }
    00109
    00110 uint32 Random()
    00111 {
    00112   _mt_left--;
    00113   if (_mt_left < 0) return ReloadRandom();
    00114
    00115   uint32 y = *_mt_next;
    00116   *_mt_next++;
    00117
    00118   y ^= (y >> 11);
    00119   y ^= (y <<  7) & 0x9D2C5680U;
    00120   y ^= (y << 15) & 0xEFC60000U;
    00121   y ^= (y >> 18);
    00122   return y;
    00123 }
    00124
    00125 #else /* MERSENNE_TWISTER */
    00126 void SetRandomSeed(uint32 seed)
    00127 {
    00128   _random.SetSeed(seed);
    00129   _interactive_random.SetSeed(seed * 0x1234567);
    00130 }
    00131
    00132 #ifdef RANDOM_DEBUG
    00133 #include "../network/network_data.h"
    00134 #include "../variables.h" /* _frame_counter */
    00135 #include "../player_func.h"
    00136
    00137 uint32 DoRandom(int line, const char *file)
    00138 {
    00139   if (_networking && (DEREF_CLIENT(0)->status != STATUS_INACTIVE || !_network_server)) {
    00140     printf("Random [%d/%d] %s:%d\n",_frame_counter, (byte)_current_player, file, line);
    00141   }
    00142
    00143   return _random.Next();
    00144 }
    00145 #endif /* RANDOM_DEBUG */
    00146 #endif /* MERSENNE_TWISTER */
    00147
    00148 #if defined(RANDOM_DEBUG) && !defined(MERSENNE_TWISTER)
    00149 uint DoRandomRange(uint max, int line, const char *file)
    00150 {
    00151   return GB(DoRandom(line, file), 0, 16) * max >> 16;
    00152 }
    00153 #endif /* RANDOM_DEBUG & !MERSENNE_TWISTER */