![]() |
Shadowrun: Awakened 29 September 2011 - Build 871
|
00001 /* 00002 Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved. 00003 00004 Redistribution and use in source and binary forms, with or without 00005 modification, are permitted provided that the following conditions are met: 00006 00007 * Redistributions of source code must retain the above copyright notice, 00008 this list of conditions and the following disclaimer. 00009 * Redistributions in binary form must reproduce the above copyright notice, 00010 this list of conditions and the following disclaimer in the documentation 00011 and/or other materials provided with the distribution. 00012 * Neither the name of LibCat nor the names of its contributors may be used 00013 to endorse or promote products derived from this software without 00014 specific prior written permission. 00015 00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00019 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00020 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00021 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00022 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00023 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00024 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00025 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00026 POSSIBILITY OF SUCH DAMAGE. 00027 */ 00028 00029 /* 00030 Algorithm by Makoto Matsumoto 00031 http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html 00032 */ 00033 00034 #ifndef CAT_MERSENNE_TWISTER_HPP 00035 #define CAT_MERSENNE_TWISTER_HPP 00036 00037 #include <cat/rand/IRandom.hpp> 00038 00039 namespace cat { 00040 00041 00042 // Noncryptographic pseudo-random number generator 00043 class MersenneTwister : public IRandom 00044 { 00045 static const u32 MEXP = 19937; 00046 static const u32 N128 = MEXP/128 + 1; 00047 static const u32 N64 = N128 * 2; 00048 static const u32 N32 = N128 * 4; 00049 static const u32 POS1 = 122; 00050 static const u32 SL1 = 18; 00051 static const u32 SL2 = 1; 00052 static const u32 SL2BITS = SL2*8; 00053 static const u32 SR1 = 11; 00054 static const u32 SR2 = 1; 00055 static const u32 SR2BITS = SR2*8; 00056 static const u32 MSK1 = 0xdfffffefU; 00057 static const u32 MSK2 = 0xddfecb7fU; 00058 static const u32 MSK3 = 0xbffaffffU; 00059 static const u32 MSK4 = 0xbffffff6U; 00060 00061 struct MT128 { 00062 u32 u[4]; 00063 }; 00064 00065 MT128 state[19937/128 + 1]; 00066 u32 *state32; 00067 u32 used; 00068 00069 CAT_INLINE void shiftLeft128(MT128 *r, MT128 *n, u32 bits); 00070 CAT_INLINE void shiftRight128(MT128 *r, MT128 *n, u32 bits); 00071 00072 void enforcePeriod(); // make corrections to ensure that the generator has the full period 00073 void round(MT128 *a, MT128 *b, MT128 *c, MT128 *d); // a = MTMIX(a,b,c,d) 00074 void update(); // permute the existing state into a new one 00075 00076 public: 00077 MersenneTwister(); 00078 00079 bool Initialize(u32 seed); 00080 bool Initialize(u32 *seeds, u32 words); 00081 bool Initialize(); 00082 00083 u32 Generate(); // generate a 32-bit number 00084 void Generate(void *buffer, int bytes); // generate a series of random numbers 00085 }; 00086 00087 00088 } // namespace cat 00089 00090 #endif // CAT_MERSENNE_TWISTER_HPP
Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.