#include "spl_heap.h"
#include "zend_exceptions.h"
#include "zend_interfaces.h"
-#include "ext/standard/php_rand.h"
#include "ext/standard/php_mt_rand.h"
#include "main/snprintf.h"
intptr_t hash_handle, hash_handlers;
if (!SPL_G(hash_mask_init)) {
- if (!BG(mt_rand_is_seeded)) {
- php_mt_srand((uint32_t)GENERATE_SEED());
- }
-
SPL_G(hash_mask_handle) = (intptr_t)(php_mt_rand() >> 1);
SPL_G(hash_mask_handlers) = (intptr_t)(php_mt_rand() >> 1);
SPL_G(hash_mask_init) = 1;
randval = php_rand();
- if ((double) (randval / (PHP_RAND_MAX + 1.0)) < (double) num_req / (double) num_avail) {
+ if ((double) (randval / PHP_RAND_MAX) <= (double) num_req / (double) num_avail) {
/* If we are returning a single result, just do it. */
if (Z_TYPE_P(return_value) != IS_ARRAY) {
if (string_key) {
ZEND_ARG_INFO(0, str)
ZEND_END_ARG_INFO()
/* }}} */
-/* {{{ rand.c */
-ZEND_BEGIN_ARG_INFO_EX(arginfo_srand, 0, 0, 0)
- ZEND_ARG_INFO(0, seed)
-ZEND_END_ARG_INFO()
-
-ZEND_BEGIN_ARG_INFO_EX(arginfo_rand, 0, 0, 0)
- ZEND_ARG_INFO(0, min)
- ZEND_ARG_INFO(0, max)
-ZEND_END_ARG_INFO()
-
-ZEND_BEGIN_ARG_INFO(arginfo_getrandmax, 0)
-ZEND_END_ARG_INFO()
-/* }}} */
/* {{{ mt_rand.c */
ZEND_BEGIN_ARG_INFO_EX(arginfo_mt_srand, 0, 0, 0)
ZEND_ARG_INFO(0, seed)
PHP_FE(proc_nice, arginfo_proc_nice)
#endif
- PHP_FE(rand, arginfo_rand)
- PHP_FE(srand, arginfo_srand)
- PHP_FE(getrandmax, arginfo_getrandmax)
- PHP_FE(mt_rand, arginfo_mt_rand)
+ PHP_FALIAS(rand, mt_rand, arginfo_mt_rand)
+ PHP_FALIAS(srand, mt_srand, arginfo_mt_srand)
+ PHP_FALIAS(getrandmax, mt_getrandmax, arginfo_mt_getrandmax)
+ PHP_FE(mt_rand, arginfo_mt_rand)
PHP_FE(mt_srand, arginfo_mt_srand)
PHP_FE(mt_getrandmax, arginfo_mt_getrandmax)
PHP_FE(mt_rand_mode, arginfo_mt_rand_mode)
static void basic_globals_ctor(php_basic_globals *basic_globals_p) /* {{{ */
{
- BG(rand_is_seeded) = 0;
BG(mt_rand_is_seeded) = 0;
BG(mt_rand_mode) = MT_RAND_MT19937;
BG(umask) = -1;
char *CurrentStatFile, *CurrentLStatFile;
php_stream_statbuf ssb, lssb;
- /* rand.c */
+ /* mt_rand.c */
uint32_t state[MT_N+1]; /* state vector + 1 extra to not violate ANSI C */
uint32_t *next; /* next random value is computed from here */
int left; /* can *next++ this many times before reloading */
- unsigned int rand_seed; /* Seed for rand(), in ts version */
-
- zend_bool rand_is_seeded; /* Whether rand() has been seeded */
zend_bool mt_rand_is_seeded; /* Whether mt_rand() has been seeded */
zend_long mt_rand_mode;
#ifndef PHP_MT_RAND_H
#define PHP_MT_RAND_H
+#include "php_lcg.h"
+#include "php_rand.h"
+
#define PHP_MT_RAND_MAX ((zend_long) (0x7FFFFFFF)) /* (1<<31) - 1 */
#define MT_RAND_MT19937 0
PHPAPI void php_mt_srand(uint32_t seed);
PHPAPI uint32_t php_mt_rand(void);
+PHPAPI zend_long php_mt_rand_range(zend_long min, zend_long max);
PHP_MINIT_FUNCTION(mt_rand);
#define PHP_RAND_H
#include "php_lcg.h"
+#include "php_mt_rand.h"
/* System Rand functions */
#ifndef RAND_MAX
-#define RAND_MAX (1<<15)
+#define RAND_MAX PHP_MT_RAND_MAX
#endif
-/* In ZTS mode we rely on rand_r() so we must use RAND_MAX. */
-#if !defined(ZTS) && (defined(HAVE_LRAND48) || defined(HAVE_RANDOM))
-#define PHP_RAND_MAX 2147483647
-#else
-#define PHP_RAND_MAX RAND_MAX
-#endif
+#define PHP_RAND_MAX PHP_MT_RAND_MAX
/*
* A bit of tricky math here. We want to avoid using a modulus because
* -RL
*/
#define RAND_RANGE(__n, __min, __max, __tmax) \
- (__n) = (__min) + (zend_long) ((double) ( (double) (__max) - (__min) + 1.0) * ((__n) / ((__tmax) + 1.0)))
+ (__n) = php_mt_rand_range((__min), (__max))
#ifdef PHP_WIN32
#define GENERATE_SEED() (((zend_long) (time(0) * GetCurrentProcessId())) ^ ((zend_long) (1000000.0 * php_combined_lcg())))
#include "php.h"
#include "php_rand.h"
-
-/* SYSTEM RAND FUNCTIONS */
+#include "php_mt_rand.h"
/* {{{ php_srand
*/
PHPAPI void php_srand(zend_long seed)
{
-#ifdef ZTS
- BG(rand_seed) = (unsigned int) seed;
-#else
-# if defined(HAVE_SRANDOM)
- srandom((unsigned int) seed);
-# elif defined(HAVE_SRAND48)
- srand48(seed);
-# else
- srand((unsigned int) seed);
-# endif
-#endif
-
- /* Seed only once */
- BG(rand_is_seeded) = 1;
+ php_mt_srand(seed);
}
/* }}} */
*/
PHPAPI zend_long php_rand(void)
{
- zend_long ret;
-
- if (!BG(rand_is_seeded)) {
- php_srand(GENERATE_SEED());
- }
-
-#ifdef ZTS
- ret = php_rand_r(&BG(rand_seed));
-#else
-# if defined(HAVE_RANDOM)
- ret = random();
-# elif defined(HAVE_LRAND48)
- ret = lrand48();
-# else
- ret = rand();
-# endif
-#endif
-
- return ret;
-}
-/* }}} */
-
-/* {{{ proto void srand([int seed])
- Seeds random number generator */
-PHP_FUNCTION(srand)
-{
- zend_long seed = 0;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &seed) == FAILURE)
- return;
-
- if (ZEND_NUM_ARGS() == 0)
- seed = GENERATE_SEED();
-
- php_srand(seed);
-}
-/* }}} */
-
-/* {{{ proto int rand([int min, int max])
- Returns a random number */
-PHP_FUNCTION(rand)
-{
- zend_long min;
- zend_long max;
- zend_long number;
- int argc = ZEND_NUM_ARGS();
-
- if (argc != 0 && zend_parse_parameters(argc, "ll", &min, &max) == FAILURE)
- return;
-
- number = php_rand();
- if (argc == 2) {
- RAND_RANGE(number, min, max, PHP_RAND_MAX);
- }
-
- RETURN_LONG(number);
-}
-/* }}} */
-
-/* {{{ proto int getrandmax(void)
- Returns the maximum value a random number can have */
-PHP_FUNCTION(getrandmax)
-{
- if (zend_parse_parameters_none() == FAILURE) {
- return;
- }
-
- RETURN_LONG(PHP_RAND_MAX);
+ return php_mt_rand();
}
/* }}} */