/* A C-program for TT800 : July 8th 1996 Version
by M. Matsumoto, email: m-mat @ math.sci.hiroshima-u.ac.jp
Modified for AVR modules by Nicolas Schodet.
genrand() generate one pseudorandom number with double precision
which is uniformly distributed on [0,1]-interval
for each call. One may choose any initial 25 seeds
except all zeros.
Copyright (C) 1996, Makoto Matsumoto
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.
See: ACM Transactions on Modelling and Computer Simulation,
Vol. 4, No. 3, 1994, pages 254-266.
*/
#include "common.h"
#define N 25
#define M 7
uint32_t
random_u32 (void)
{
uint32_t y;
static uint8_t k = 0;
static uint32_t x[N]={ /* initial 25 seeds, change as you wish */
0x95f24dab, 0x0b685215, 0xe76ccae7, 0xaf3ec239, 0x715fad23,
0x24a590ad, 0x69e4b5ef, 0xbf456141, 0x96bc1b7b, 0xa7bdf825,
0xc1de75b7, 0x8858a9c9, 0x2da87693, 0xb657f9dd, 0xffdc8a9f,
0x8121da71, 0x8b823ecb, 0x885d05f5, 0x4e20cd47, 0x5a9ad5d9,
0x512c0c03, 0xea857ccd, 0x4cc1d30f, 0x8891a8a1, 0xa6b7aadb
};
static uint32_t mag01[2]={
0x0, 0x8ebfd028 /* this is magic vector `a', don't change */
};
if (k==N) { /* generate N words at one time */
uint8_t kk;
for (kk=0;kk<N-M;kk++) {
x[kk] = x[kk+M] ^ (x[kk] >> 1) ^ mag01[x[kk] % 2];
}
for (; kk<N;kk++) {
x[kk] = x[kk+(M-N)] ^ (x[kk] >> 1) ^ mag01[x[kk] % 2];
}
k=0;
}
y = x[k];
y ^= (y << 7) & 0x2b5b2500; /* s and b, magic vectors */
y ^= (y << 15) & 0xdb8b0000; /* t and c, magic vectors */
/*y &= 0xffffffff;*/ /* you may delete this line if word size = 32 */
/*
the following line was added by Makoto Matsumoto in the 1996 version
to improve lower bit's corellation.
Delete this line to o use the code published in 1994.
*/
y ^= (y >> 16); /* added to the 1994 version */
k++;
return y;
}
And the second version:
uint32_t random_u32 (void)
{
uint32_t y;
static uint8_t k = 0;
static uint32_t x[25] = {
0x95f24dab, 0x0b685215, 0xe76ccae7, 0xaf3ec239, 0x715fad23,
0x24a590ad, 0x69e4b5ef, 0xbf456141, 0x96bc1b7b, 0xa7bdf825,
0xc1de75b7, 0x8858a9c9, 0x2da87693, 0xb657f9dd, 0xffdc8a9f,
0x8121da71, 0x8b823ecb, 0x885d05f5, 0x4e20cd47, 0x5a9ad5d9,
0x512c0c03, 0xea857ccd, 0x4cc1d30f, 0x8891a8a1, 0xa6b7aadb
};
y = x[k];
uint32_t lfsr = (y >> 1);
if (y & 0x00000001) lfsr ^= 0x8ebfd028;
uint8_t n = k - 18;
if (n < 0) n += 25;
x[k++] = x[n] ^ lfsr;
if (k >= 25) k = 0;
y ^= (y << 7) & 0x2b5b2500;
y ^= (y << 15) & 0xdb8b0000;
y ^= (y >> 16);
return y;
}