Skip to content

Instantly share code, notes, and snippets.

@qtxie
Created June 22, 2013 02:36
Show Gist options
  • Save qtxie/5835613 to your computer and use it in GitHub Desktop.
Save qtxie/5835613 to your computer and use it in GitHub Desktop.
Mersenne Twister algorithm C implementation corresponding to the Red/System implementation. Use to do benchmark with the Red/System implement.
#include <stdio.h>
/* Mersenne Twister magic numbers */
#define _RAND_N 624
#define _RAND_M 397
#define _RAND_U 0x80000000L
#define _RAND_L 0x7fffffffL
/* Mersenne Twister state and seed storage */
static int rand_state[_RAND_N];
static int rand_next;
/*
@description:
Seeds the Mersenne Twister state.
*/
void _srand(unsigned seed)
{
int i;
rand_state[0] = seed;
for (i = 1; i < _RAND_N; ++i)
rand_state[i] = (1812433253L * (rand_state[i - 1] ^ (rand_state[i - 1] >> 30)) + i);
}
/*
@description:
Generates a random number in the range of [0,RAND_MAX)
using the Mersenne Twister algorithm.
*/
int _rand(void)
{
int y, i;
/* Refill rand_state if exhausted */
if (rand_next == _RAND_N) {
rand_next = 0;
for (i = 0; i < _RAND_N - 1; i++) {
y = (rand_state[i] & _RAND_U) | rand_state[i + 1] & _RAND_L;
rand_state[i] = rand_state[(i + _RAND_M) % _RAND_N] ^ (y >> 1) ^ ((y & 1L) ? 0x9908b0dfL : 0x0UL);
}
y = (rand_state[_RAND_N - 1] & _RAND_U) | rand_state[0] & _RAND_L;
rand_state[_RAND_N - 1] = rand_state[_RAND_M - 1] ^ (y >> 1) ^ ((y & 0x1UL) ? 0x9908b0dfL : 0L);
}
y = rand_state[rand_next++];
/* Add a little extra mixing */
y ^= (y >> 11);
y ^= (y << 7) & 0x9d2c5680UL;
y ^= (y << 15) & 0xefc60000UL;
y ^= (y >> 18);
return y;
}
int main()
{
int i = 0;
int temp;
printf("--Test--\n");
_srand(42);
while (i < 99999999) {
temp = _rand() % 999;
i++;
}
printf("%d\n", _rand() % 999);
printf("%d\n", _rand() % 999);
printf("%d\n", _rand() % 999);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment