ㅤ Developer
#include "php.h"
#include "php_mt_rand.h"
/* Mersenne Twister */
#define N 624
#define M 397
#define MATRIX_A 0x9908b0dfUL /* constant vector a */
#define UPPER_MASK 0x80000000UL /* most significant w-r bits */
#define LOWER_MASK 0x7fffffffUL /* least significant r bits */
static unsigned long mt[N]; /* the array for the state vector */
static int mti = N + 1; /* mti==N+1 means mt[N] is not initialized */
static void php_mt_initialize(unsigned long s)
{
mt[0] = s & 0xffffffffUL;
for (mti = 1; mti < N; mti++) {
mt[mti] =
(1812433253UL * (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + mti);
mt[mti] &= 0xffffffffUL;
}
}
static void php_mt_reload(void)
{
int i;
unsigned long y;
for (i = 0; i < N - M; i++) {
y = (mt[i] & UPPER_MASK) | (mt[i + 1] & LOWER_MASK);
mt[i] = mt[i + M] ^ (y >> 1) ^ (-(y & 1) & MATRIX_A);
}
for (; i < N - 1; i++) {
y = (mt[i] & UPPER_MASK) | (mt[i + 1] & LOWER_MASK);
mt[i] = mt[i + (M - N)] ^ (y >> 1) ^ (-(y & 1) & MATRIX_A);
}
y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ (-(y & 1) & MATRIX_A);
mti = 0;
}
static unsigned long php_mt_rand()
{
unsigned long y;
static unsigned long mag01[2] = {0x0UL, MATRIX_A};
if (mti >= N) { /* generate N words at one time */
if (mti == N + 1) /* if php_mt_initialize() has not been called, */
php_mt_initialize(5489UL); /* a default initial seed is used */
php_mt_reload();
}
y = mt[mti++];
/* Tempering */
y ^= (y >> 11);
y ^= (y 18);
return y;
}
PHPAPI long php_mt_rand_range(long min, long max)
{
unsigned long umax = (unsigned long) max - min;
if (umax == 0) {
return min;
} else if (umax max) {
zend_argument_value_error(1, "must be less than or equal to the second argument");
return;
}
RETURN_LONG(php_mt_rand_range(min, max));
}
PHP_FUNCTION(mt_srand)
{
zend_long seed = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &seed) == FAILURE) {
return;
}
if (ZEND_NUM_ARGS() == 0) {
struct timeval tv;
gettimeofday(&tv, NULL);
seed = (long) (tv.tv_sec ^ (tv.tv_usec
#include "php_mt_rand.h"
/* Mersenne Twister */
#define N 624
#define M 397
#define MATRIX_A 0x9908b0dfUL /* constant vector a */
#define UPPER_MASK 0x80000000UL /* most significant w-r bits */
#define LOWER_MASK 0x7fffffffUL /* least significant r bits */
static unsigned long mt[N]; /* the array for the state vector */
static int mti = N + 1; /* mti==N+1 means mt[N] is not initialized */
static void php_mt_initialize(unsigned long s)
{
mt[0] = s & 0xffffffffUL;
for (mti = 1; mti < N; mti++) {
mt[mti] =
(1812433253UL * (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + mti);
mt[mti] &= 0xffffffffUL;
}
}
static void php_mt_reload(void)
{
int i;
unsigned long y;
for (i = 0; i < N - M; i++) {
y = (mt[i] & UPPER_MASK) | (mt[i + 1] & LOWER_MASK);
mt[i] = mt[i + M] ^ (y >> 1) ^ (-(y & 1) & MATRIX_A);
}
for (; i < N - 1; i++) {
y = (mt[i] & UPPER_MASK) | (mt[i + 1] & LOWER_MASK);
mt[i] = mt[i + (M - N)] ^ (y >> 1) ^ (-(y & 1) & MATRIX_A);
}
y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ (-(y & 1) & MATRIX_A);
mti = 0;
}
static unsigned long php_mt_rand()
{
unsigned long y;
static unsigned long mag01[2] = {0x0UL, MATRIX_A};
if (mti >= N) { /* generate N words at one time */
if (mti == N + 1) /* if php_mt_initialize() has not been called, */
php_mt_initialize(5489UL); /* a default initial seed is used */
php_mt_reload();
}
y = mt[mti++];
/* Tempering */
y ^= (y >> 11);
y ^= (y 18);
return y;
}
PHPAPI long php_mt_rand_range(long min, long max)
{
unsigned long umax = (unsigned long) max - min;
if (umax == 0) {
return min;
} else if (umax max) {
zend_argument_value_error(1, "must be less than or equal to the second argument");
return;
}
RETURN_LONG(php_mt_rand_range(min, max));
}
PHP_FUNCTION(mt_srand)
{
zend_long seed = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &seed) == FAILURE) {
return;
}
if (ZEND_NUM_ARGS() == 0) {
struct timeval tv;
gettimeofday(&tv, NULL);
seed = (long) (tv.tv_sec ^ (tv.tv_usec