Last active
August 29, 2015 14:03
-
-
Save Slipyx/209e6fabbce896d344f8 to your computer and use it in GitHub Desktop.
MT19937 with improved initialization, optimization, and simplification.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
MT19937 with initialization improved 2002/2/10. | |
Coded by Takuji Nishimura and Makoto Matsumoto. | |
Faster version using Shawn Cokus's optimization and | |
Matthe Bellew's simplification. | |
Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions | |
are met: | |
1. Redistributions of source code must retain the above copyright | |
notice, this list of conditions and the following disclaimer. | |
2. Redistributions in binary form must reproduce the above copyright | |
notice, this list of conditions and the following disclaimer in the | |
documentation and/or other materials provided with the distribution. | |
3. The names of its contributors may not be used to endorse or promote | |
products derived from this software without specific prior written | |
permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html | |
Modifications to original: Hardcoded period parameters. Implemented minimal | |
stdint. Removed initialization by array. Removed initialization check. Removed | |
real versions. Stripped comments and general formatting cleanup. Made state | |
variables into a struct. Renamed functions to be more consistent. Changed | |
next_state to use state's counter variable. Inlined next_state inside genrand. | |
Removed next pointer, only using state counter. Added macro for random doubles | |
in range [0-1]. Modified TWIST macro for minor speedup. Strict ANSI C. | |
*/ | |
#ifndef __MTARCOK_H | |
#define __MTARCOK_H | |
/* minimal stdint implementation */ | |
typedef signed short int16_t; | |
typedef unsigned long uint32_t; | |
/* state struct */ | |
typedef struct { | |
uint32_t s[624]; | |
int16_t i; | |
} mtstate_t; | |
/* global state object */ | |
static mtstate_t mt = {{0}, 624}; | |
/* initialize state with single seed. MUST be called before use */ | |
void mt_init( uint32_t s ) { | |
int16_t j; | |
mt.s[0] = s & 0xffffffffUL; | |
for ( j = 1; j < 624; j++ ) { | |
mt.s[j] = (1812433253UL * (mt.s[j - 1] ^ (mt.s[j - 1] >> 30)) + j); | |
mt.s[j] &= 0xffffffffUL; | |
} | |
mt.i = 624; | |
} | |
#define TWIST( u, v ) ((((u & 0x80000000UL) | (v & 0x7fffffffUL)) >> 1) ^ ((v & 1UL) * 0x9908b0dfUL)) | |
/* random unsigned 32bit integer */ | |
uint32_t mt_rand( void ) { | |
uint32_t y; | |
/* generate the next state if needed */ | |
if ( mt.i == 624 ) { | |
uint32_t* p = mt.s; | |
for ( mt.i = 228; --mt.i; p++ ) | |
*p = p[397] ^ TWIST( p[0], p[1] ); | |
for ( mt.i = 397; --mt.i; p++ ) | |
*p = p[-227] ^ TWIST( p[0], p[1] ); | |
*p = p[-227] ^ TWIST( p[0], p[0] ); | |
mt.i = 0; | |
} | |
y = mt.s[mt.i++]; | |
y ^= (y >> 11); | |
y ^= (y << 7) & 0x9d2c5680UL; | |
y ^= (y << 15) & 0xefc60000UL; | |
y ^= (y >> 18); | |
return y; | |
} | |
/* random double [0-1] */ | |
#define mt_random() (mt_rand() * (1.0 / 0xffffffffUL)) | |
#endif /* __MTARCOK_H */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment