cvs: php4 /ext/standard php_rand.h rand.c

From: Date: Sun, 09 Sep 2001 10:44:12 +0000
Subject: cvs: php4 /ext/standard php_rand.h rand.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-7328@lists.php.net to get a copy of this message
sterling Sun Sep 9 06:44:12 2001 EDT Modified files: /php4/ext/standard rand.c php_rand.h Log: export the mt rand functions... Index: php4/ext/standard/rand.c diff -u php4/ext/standard/rand.c:1.48 php4/ext/standard/rand.c:1.49 --- php4/ext/standard/rand.c:1.48 Thu Sep 6 03:33:45 2001 +++ php4/ext/standard/rand.c Sun Sep 9 06:44:11 2001 @@ -19,7 +19,7 @@ | Based on code from: Shawn Cokus <Cokus@math.washington.edu> | +----------------------------------------------------------------------+ */ -/* $Id: rand.c,v 1.48 2001/09/06 07:33:45 sterling Exp $ */ +/* $Id: rand.c,v 1.49 2001/09/09 10:44:11 sterling Exp $ */ #include <stdlib.h> @@ -92,11 +92,9 @@ #define loBits(u) ((u) & 0x7FFFFFFFU) /* mask the highest bit of u */ #define mixBits(u, v) (hiBit(u)|loBits(v)) /* move hi bit of u to hi bit of v */ -#define MT_RAND_MAX ((long)(0x7FFFFFFF)) /* (1<<31) - 1 */ - -/* {{{ seedMT +/* {{{ php_mt_srand */ -static void seedMT(php_uint32 seed TSRMLS_DC) +PHPAPI void php_mt_srand(php_uint32 seed TSRMLS_DC) { /* We initialize state[0..(N-1)] via the generator @@ -158,7 +156,7 @@ register int j; if (BG(left) < -1) - seedMT(4357U TSRMLS_CC); + php_mt_srand(4357U TSRMLS_CC); BG(left) = N - 1, BG(next) = BG(state) + 1; @@ -177,7 +175,7 @@ } -static php_uint32 randomMT(TSRMLS_D) +PHPAPI php_uint32 php_mt_rand(TSRMLS_D) { php_uint32 y; @@ -214,7 +212,7 @@ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &seed) == FAILURE) return; - seedMT(seed TSRMLS_CC); + php_mt_srand(seed TSRMLS_CC); } /* }}} */ @@ -244,8 +242,8 @@ * * -RL */ -#define RAND_RANGE(__n, __min, __max) \ - (__n) = (__min) + (long) ((double) ((__max) - (__min) + 1.0) * ((__n) / (PHP_RAND_MAX + 1.0))) +#define RAND_RANGE(__n, __min, __max, __tmax) \ + (__n) = (__min) + (long) ((double) ((__max) - (__min) + 1.0) * ((__n) / ((__tmax) + 1.0))) /* {{{ proto int rand([int min, int max]) Returns a random number */ @@ -261,7 +259,7 @@ number = php_rand(); if (argc == 2) { - RAND_RANGE(number, min, max); + RAND_RANGE(number, min, max, PHP_RAND_MAX); } RETURN_LONG(number); @@ -288,9 +286,9 @@ * Update: * I talked with Cokus via email and it won't ruin the algorithm */ - number = (long) (randomMT(TSRMLS_C) >> 1); + number = (long) (php_mt_rand(TSRMLS_C) >> 1); if (argc == 2) { - RAND_RANGE(number, min, max); + RAND_RANGE(number, min, max, PHP_MT_RAND_MAX); } RETURN_LONG(number); @@ -321,7 +319,7 @@ * Melo: it could be 2^^32 but we only use 2^^31 to maintain * compatibility with the previous php_rand */ - RETURN_LONG(MT_RAND_MAX); /* 2^^31 */ + RETURN_LONG(PHP_MT_RAND_MAX); /* 2^^31 */ } /* }}} */ Index: php4/ext/standard/php_rand.h diff -u php4/ext/standard/php_rand.h:1.14 php4/ext/standard/php_rand.h:1.15 --- php4/ext/standard/php_rand.h:1.14 Wed Sep 5 15:47:55 2001 +++ php4/ext/standard/php_rand.h Sun Sep 9 06:44:12 2001 @@ -19,13 +19,15 @@ | Based on code from: Shawn Cokus <Cokus@math.washington.edu> | +----------------------------------------------------------------------+ */ -/* $Id: php_rand.h,v 1.14 2001/09/05 19:47:55 sniper Exp $ */ +/* $Id: php_rand.h,v 1.15 2001/09/09 10:44:12 sterling Exp $ */ #ifndef PHP_RAND_H #define PHP_RAND_H #include <stdlib.h> +#include "basic_functions.h" +/* System Rand functions */ #ifndef RAND_MAX #define RAND_MAX (1<<15) #endif @@ -57,5 +59,11 @@ #define php_srand(seed) srand((unsigned int)seed) #endif #endif + +/* MT Rand */ +#define PHP_MT_RAND_MAX ((long)(0x7FFFFFFF)) /* (1<<31) - 1 */ + +PHPAPI void php_mt_srand(php_uint32 seed TSRMLS_DC); +PHPAPI php_uint32 php_mt_rand(TSRMLS_D); #endif /* PHP_RAND_H */

« previous php.cvs (#7328) next »