static int array_data_shuffle(const void *a, const void*b) {
- return (php_rand() % 2) ? 1 : -1;
+ TSRMLS_FETCH();
+ return (php_rand(TSRMLS_C) % 2) ? 1 : -1;
}
* tab-width: 4
* c-basic-offset: 4
* End:
- * vim600: sw=4 ts=4 fdm=marker
- * vim<600: sw=4 ts=4
+ * vim600: noet sw=4 ts=4 fdm=marker
+ * vim<600: noet sw=4 ts=4
*/
php_uint32 *next; /* next random value is computed from here */
int left; /* can *next++ this many times before reloading */
+ unsigned int rand_seed; /* Seed for rand() */
+
/* syslog.c */
int syslog_started;
char *syslog_device;
#endif
-#define PHP_CRYPT_RAND php_rand()
+#define PHP_CRYPT_RAND php_rand(TSRMLS_C)
static int php_crypt_rand_seeded=0;
PHP_RINIT_FUNCTION(crypt)
{
if(!php_crypt_rand_seeded) {
- php_srand(time(0) * getpid() * (php_combined_lcg(TSRMLS_C) * 10000.0));
+ php_srand(time(0) * getpid() * (php_combined_lcg(TSRMLS_C) * 10000.0) TSRMLS_CC);
php_crypt_rand_seeded=1;
}
return SUCCESS;
static unsigned char itoa64[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
-static void php_to64(char *s, long v, int n) {
+static void php_to64(char *s, long v, int n)
+{
while (--n >= 0) {
*s++ = itoa64[v&0x3f];
v >>= 6;
| Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
| Zeev Suraski <zeev@zend.com> |
| Pedro Melo <melo@ip.pt> |
+ | Sterling Hughes <sterling@php.net> |
| |
| Based on code from: Shawn Cokus <Cokus@math.washington.edu> |
+----------------------------------------------------------------------+
#define PHP_RAND_MAX RAND_MAX
#endif
-/* Define rand Function wrapper */
-#ifdef HAVE_RANDOM
-#define php_rand() random()
-#else
-#ifdef HAVE_LRAND48
-#define php_rand() lrand48()
-#else
-#define php_rand() rand()
-#endif
-#endif
-
-/* Define srand Function wrapper */
-#ifdef HAVE_SRANDOM
-#define php_srand(seed) srandom((unsigned int)seed)
-#else
-#ifdef HAVE_SRAND48
-#define php_srand(seed) srand48((long)seed)
-#else
-#define php_srand(seed) srand((unsigned int)seed)
-#endif
-#endif
-
/* MT Rand */
-#define PHP_MT_RAND_MAX ((long)(0x7FFFFFFF)) /* (1<<31) - 1 */
+#define PHP_MT_RAND_MAX ((long) (0x7FFFFFFF)) /* (1<<31) - 1 */
+PHPAPI void php_srand(long seed TSRMLS_DC);
+PHPAPI long php_rand(TSRMLS_D);
PHPAPI void php_mt_srand(php_uint32 seed TSRMLS_DC);
PHPAPI php_uint32 php_mt_rand(TSRMLS_D);
#include <stdlib.h>
#ifdef PHP_WIN32
-#include <windows.h>
+# ifndef WIN32_LEAN_AND_MEAN
+# define WIN32_LEAN_AND_MEAN
+# endif
+# include <windows.h>
#endif
#include "php.h"
#include "basic_functions.h"
+
+/* SYSTEM RAND FUNCTIONS */
+
+/* {{{ php_srand
+ */
+PHPAPI void php_srand(long seed TSRMLS_DC)
+{
+#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
+}
+/* }}} */
+
+/* {{{ php_rand
+ */
+PHPAPI long php_rand(TSRMLS_D)
+{
+ long ret;
+
+#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;
+}
+/* }}} */
+
+
+/* MT RAND FUNCTIONS */
+
/*
This is the ``Mersenne Twister'' random number generator MT19937, which
generates pseudorandom integers uniformly distributed in 0..(2^32 - 1)
Melo: we should put some ifdefs here to catch those alphas...
*/
-
-
#define N MT_N /* length of state vector */
#define M (397) /* a period parameter */
#define K (0x9908B0DFU) /* a magic constant */
}
/* }}} */
-static php_uint32 reloadMT(TSRMLS_D)
+/* {{{ php_mt_reload
+ */
+static php_uint32 php_mt_reload(TSRMLS_D)
{
register php_uint32 *p0 = BG(state), *p2 = BG(state) + 2, *pM = BG(state) + M, s0, s1;
register int j;
return s1 ^ (s1 >> 18);
}
+/* }}} */
-
+/* {{{ php_mt_rand
+ */
PHPAPI php_uint32 php_mt_rand(TSRMLS_D)
{
php_uint32 y;
if (--BG(left) < 0)
- return reloadMT(TSRMLS_C);
+ return php_mt_reload(TSRMLS_C);
y = *BG(next)++;
y ^= (y >> 11);
return y ^ (y >> 18);
}
+/* }}} */
#ifdef PHP_WIN32
#define GENERATE_SEED() (time(0) * GetCurrentProcessId() * 1000000 * php_combined_lcg(TSRMLS_C))
if (ZEND_NUM_ARGS() == 0)
seed = GENERATE_SEED();
- php_srand(seed);
+ php_srand(seed TSRMLS_CC);
}
/* }}} */
if (argc != 0 && zend_parse_parameters(argc TSRMLS_CC, "ll", &min, &max) == FAILURE)
return;
- number = php_rand();
+ number = php_rand(TSRMLS_C);
+
if (argc == 2) {
RAND_RANGE(number, min, max, PHP_RAND_MAX);
}