cvs: php4(RAND_REDESIGN) /ext/standard php_rand.h rand.c
| From: | Jeroen van Wolffelaar | Date: | Mon, 03 Sep 2001 00:29:54 +0000 |
| Subject: | cvs: php4(RAND_REDESIGN) /ext/standard php_rand.h rand.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-7157@lists.php.net to get a copy of this message | ||
jeroen Sun Sep 2 20:29:54 2001 EDT
Modified files: (Branch: RAND_REDESIGN)
/php4/ext/standard php_rand.h rand.c
Log:
Implement the proposal v2
Added PHPAPI php_drand, which returns a double in the range [0,1)
RAND_REDESIGN completed, prohibited some minor points. Ready to
be re-entered in MAIN branch
Index: php4/ext/standard/php_rand.h
diff -u php4/ext/standard/php_rand.h:1.8.8.5 php4/ext/standard/php_rand.h:1.8.8.6
--- php4/ext/standard/php_rand.h:1.8.8.5 Sun Sep 2 19:39:45 2001
+++ php4/ext/standard/php_rand.h Sun Sep 2 20:29:53 2001
@@ -20,7 +20,7 @@
| Based on code from: Shawn Cokus <Cokus@math.washington.edu> |
+----------------------------------------------------------------------+
*/
-/* $Id: php_rand.h,v 1.8.8.5 2001/09/02 23:39:45 jeroen Exp $ */
+/* $Id: php_rand.h,v 1.8.8.6 2001/09/03 00:29:53 jeroen Exp $ */
/* Layout implementation random functions
*
@@ -113,9 +113,10 @@
PHP_FUNCTION(mt_rand);
PHP_FUNCTION(mt_getrandmax);
-PHPAPI long php_rand(void);
-PHPAPI long php_rand_range(long min, long max);
-PHPAPI long php_randmax(void);
+PHPAPI long php_rand(void);
+PHPAPI long php_rand_range(long min, long max);
+PHPAPI double php_drand(void);
+PHPAPI long php_randmax(void);
#endif /* PHP_RAND_H */
Index: php4/ext/standard/rand.c
diff -u php4/ext/standard/rand.c:1.31.4.5 php4/ext/standard/rand.c:1.31.4.6
--- php4/ext/standard/rand.c:1.31.4.5 Sun Sep 2 19:39:45 2001
+++ php4/ext/standard/rand.c Sun Sep 2 20:29:54 2001
@@ -17,7 +17,7 @@
| Pedro Melo <melo@ip.pt> |
+----------------------------------------------------------------------+
*/
-/* $Id: rand.c,v 1.31.4.5 2001/09/02 23:39:45 jeroen Exp $ */
+/* $Id: rand.c,v 1.31.4.6 2001/09/03 00:29:54 jeroen Exp $ */
#include <stdlib.h>
@@ -41,6 +41,8 @@
#define PHP_RANDMAX(which) (php_randgen_entries[which]->randmax)
#define PHP_RAND_INISTR(which) (php_randgen_entries[which]->ini_str)
+#define CURR_GEN BG(rand_generator_current)
+
PHP_MINIT_FUNCTION(rand)
{
PHP_MINIT(rand_sys)(INIT_FUNC_ARGS_PASSTHRU);
@@ -110,7 +112,7 @@
/* {{{ PHPAPI void php_srand(void) */
PHPAPI void php_srand(void)
{
- BG(rand_generator_current) = BG(rand_generator);
+ CURR_GEN = BG(rand_generator);
PHP_SRAND(BG(rand_generator), SRAND_A_RANDOM_SEED);
}
/* }}} */
@@ -119,25 +121,49 @@
#define pim_srand_common(name,type) \
PHP_FUNCTION(name) \
{ \
- zval **arg; \
- \
- if (ZEND_NUM_ARGS() != 1) { \
- WRONG_PARAM_COUNT; \
- } \
- zend_get_parameters_ex(1, &arg); \
- convert_to_long_ex(arg); \
+ zval **seed; \
+ zval **alg; \
\
- BG(rand_generator_current) = type; \
- PHP_SRAND(type, Z_LVAL_PP(arg)); \
+ switch (ZEND_NUM_ARGS()) { \
+ case 0: \
+ CURR_GEN = BG(rand_generator); \
+ PHP_SRAND(BG(rand_generator), SRAND_A_RANDOM_SEED); \
+ RETURN_TRUE; \
+ case 1: \
+ zend_get_parameters_ex(1, &seed); \
+ convert_to_long_ex(seed); \
+ CURR_GEN = type; \
+ PHP_SRAND(type, Z_LVAL_PP(seed)); \
+ RETURN_TRUE; \
+ case 2: \
+ /* algorithm, seed is most logic, though it is the other way
+ * around than current way... */ \
+ zend_get_parameters_ex(2, &alg, &seed); \
+ convert_to_long_ex(seed); \
+ convert_to_long_ex(alg); \
+ if (0 > Z_LVAL_PP(alg) || Z_LVAL_PP(alg) >= PHP_RAND_NUMRANDS) { \
+ php_error(E_WARNING, "%s(): There is no algorithm %d.",
get_active_function_name(TSRMLS_C), Z_LVAL_PP(alg)); \
+ RETURN_FALSE; \
+ } \
+ if (!PHP_HAS_SRAND(Z_LVAL_PP(alg))) { \
+ php_error(E_WARNING, "%s(): Algorithm %d does not support reproducable results.",
get_active_function_name(TSRMLS_C), Z_LVAL_PP(alg)); \
+ RETURN_FALSE; \
+ } \
+ CURR_GEN = Z_LVAL_PP(alg); \
+ PHP_SRAND(Z_LVAL_PP(alg), Z_LVAL_PP(seed)); \
+ RETURN_TRUE; \
+ default: \
+ WRONG_PARAM_COUNT; \
+ } \
}
/* }}} */
-/* {{{ proto void srand(int seed)
+/* {{{ proto bool srand(int seed)
Seeds random number generator */
pim_srand_common(srand,PHP_RAND_SYS)
/* }}} */
-/* {{{ proto void mt_srand(int seed)
+/* {{{ proto bool mt_srand(int seed)
Seeds random number generator */
pim_srand_common(mt_srand,PHP_RAND_MT)
/* }}} */
@@ -147,7 +173,16 @@
/* {{{ PHPAPI long php_rand(void) */
PHPAPI long php_rand(void)
{
- return PHP_RAND(BG(rand_generator_current));
+ return PHP_RAND(CURR_GEN);
+}
+/* }}} */
+
+/* {{{ PHPAPI double php_drand(void)
+ * returns a double in the range [0,1) */
+PHPAPI double php_drand(void)
+{
+ return (double)php_rand() /
+ (double)(PHP_RANDMAX(CURR_GEN)+1.0);
}
/* }}} */
@@ -192,7 +227,8 @@
PHPAPI long php_rand_range(long min, long max)
{
register long result;
- PHP_RAND_RANGE(BG(rand_generator_current), min, max, result);
+
+ PHP_RAND_RANGE(CURR_GEN, min, max, result);
return result;
}
/* }}} */
@@ -239,7 +275,7 @@
Returns the maximum value a random number can have */
PHPAPI long php_randmax(void)
{
- return PHP_RANDMAX(BG(rand_generator_current));
+ return PHP_RANDMAX(CURR_GEN);
}
/* }}} */
@@ -251,7 +287,7 @@
WRONG_PARAM_COUNT;
}
- RETURN_LONG( PHP_RANDMAX(PHP_RAND_SYS));
+ RETURN_LONG( php_randmax());
}
/* }}} */
@@ -263,7 +299,7 @@
WRONG_PARAM_COUNT;
}
- RETURN_LONG( PHP_RANDMAX(PHP_RAND_MT));
+ RETURN_LONG( php_randmax() );
}
/* }}} */