• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

ink_rand.cc

Go to the documentation of this file.
00001 /** @file
00002 
00003    A C-program for MT19937-64 (2004/9/29 version).
00004    Coded by Takuji Nishimura and Makoto Matsumoto.
00005 
00006    This is a 64-bit version of Mersenne Twister pseudorandom number
00007    generator.
00008 
00009    Before using, initialize the state by using init_genrand64(seed)  
00010    or init_by_array64(init_key, key_length).
00011 
00012    Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura,
00013    All rights reserved.                          
00014 
00015    Redistribution and use in source and binary forms, with or without
00016    modification, are permitted provided that the following conditions
00017    are met:
00018 
00019      1. Redistributions of source code must retain the above copyright
00020         notice, this list of conditions and the following disclaimer.
00021 
00022      2. Redistributions in binary form must reproduce the above copyright
00023         notice, this list of conditions and the following disclaimer in the
00024         documentation and/or other materials provided with the distribution.
00025 
00026      3. The names of its contributors may not be used to endorse or promote 
00027         products derived from this software without specific prior written 
00028         permission.
00029 
00030    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00031    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00032    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00033    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
00034    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00035    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00036    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00037    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00038    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00039    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00040    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00041 
00042    References:
00043    T. Nishimura, ``Tables of 64-bit Mersenne Twisters''
00044      ACM Transactions on Modeling and 
00045      Computer Simulation 10. (2000) 348--357.
00046    M. Matsumoto and T. Nishimura,
00047      ``Mersenne Twister: a 623-dimensionally equidistributed
00048        uniform pseudorandom number generator''
00049      ACM Transactions on Modeling and 
00050      Computer Simulation 8. (Jan. 1998) 3--30.
00051 
00052    Any feedback is very welcome.
00053    http://www.math.hiroshima-u.ac.jp/~m-mat/MT/emt.html
00054    email: m-mat @ math.sci.hiroshima-u.ac.jp (remove spaces)
00055 */
00056 
00057 
00058 #include "libts.h"
00059 
00060 #define NN 312
00061 #define MM 156
00062 #define MATRIX_A 0xB5026F5AA96619E9ULL
00063 #define UM 0xFFFFFFFF80000000ULL /* Most significant 33 bits */
00064 #define LM 0x7FFFFFFFULL /* Least significant 31 bits */
00065 
00066 static uint64_t mag01[2]={0ULL, MATRIX_A};
00067 
00068 InkRand::InkRand(uint64_t d) {
00069   seed(d);
00070 }
00071 
00072 void InkRand::seed(uint64_t seed) {
00073   mt[0] = seed;
00074   for (mti=1; mti<NN; mti++) 
00075     mt[mti] =  (6364136223846793005ULL * (mt[mti-1] ^ (mt[mti-1] >> 62)) + mti);
00076 }
00077 
00078 uint64_t InkRand::random() {
00079   int i;
00080   uint64_t x;
00081 
00082   if (mti >= NN) { /* generate NN words at one time */
00083     for (i=0;i<NN-MM;i++) {
00084       x = (mt[i]&UM)|(mt[i+1]&LM);
00085       mt[i] = mt[i+MM] ^ (x>>1) ^ mag01[(int)(x&1ULL)];
00086     }
00087     for (;i<NN-1;i++) {
00088       x = (mt[i]&UM)|(mt[i+1]&LM);
00089       mt[i] = mt[i+(MM-NN)] ^ (x>>1) ^ mag01[(int)(x&1ULL)];
00090     }
00091     x = (mt[NN-1]&UM)|(mt[0]&LM);
00092     mt[NN-1] = mt[MM-1] ^ (x>>1) ^ mag01[(int)(x&1ULL)];
00093 
00094     mti = 0;
00095   }
00096 
00097   x = mt[mti++];
00098 
00099   x ^= (x >> 29) & 0x5555555555555555ULL;
00100   x ^= (x << 17) & 0x71D67FFFEDA60000ULL;
00101   x ^= (x << 37) & 0xFFF7EEE000000000ULL;
00102   x ^= (x >> 43);
00103 
00104   return x;
00105 }
00106 
00107 double InkRand::drandom() {
00108   return (random() >> 11) * (1.0/9007199254740991.0);
00109 }

Generated by  doxygen 1.7.1