cvs: /php4/ext/standard rand.c

From: Date: Sat, 24 Jul 1999 02:30:37 +0000
Subject: cvs: /php4/ext/standard rand.c
Groups: php.version4 
Request: Send a blank email to php-version4+get-2665@lists.php.net to get a copy of this message
rasmus Fri Jul 23 22:30:37 1999 EDT Modified files: /php4/ext/standard rand.c Log: Bring rand.c up to date Index: php4/ext/standard/rand.c diff -u php4/ext/standard/rand.c:1.3 php4/ext/standard/rand.c:1.4 --- php4/ext/standard/rand.c:1.3 Fri Jul 16 09:12:57 1999 +++ php4/ext/standard/rand.c Fri Jul 23 22:30:37 1999 @@ -19,7 +19,7 @@ | Based on code from: Shawn Cokus <Cokus@math.washington.edu> | +----------------------------------------------------------------------+ */ -/* $Id: rand.c,v 1.3 1999/07/16 13:12:57 zeev Exp $ */ +/* $Id: rand.c,v 1.4 1999/07/24 02:30:37 rasmus Exp $ */ #include <stdlib.h> @@ -201,6 +201,8 @@ #define PHP_RAND_MAX RAND_MAX #endif +/* {{{ proto void srand(int seed) + Seeds random number generator */ PHP_FUNCTION(srand) { pval *arg; @@ -219,7 +221,10 @@ #endif #endif } +/* }}} */ +/* {{{ proto void mt_srand(int seed) + Seeds Mersenne Twister random number generator */ PHP_FUNCTION(mt_srand) { pval *arg; @@ -229,7 +234,10 @@ convert_to_long(arg); seedMT(arg->value.lval); } +/* }}} */ +/* {{{ proto int rand([int min, int max]) + Returns a random number */ PHP_FUNCTION(rand) { pval *p_min=NULL, *p_max=NULL; @@ -262,13 +270,40 @@ return_value->value.lval = rand(); #endif #endif - + /* + * A bit of tricky math here. We want to avoid using a modulus because + * that simply tosses the high-order bits and might skew the distribution + * of random values over the range. Instead we map the range directly. + * + * We need to map the range from 0...M evenly to the range a...b + * Let n = the random number and n' = the mapped random number + * + * Then we have: n' = a + n(b-a)/M + * + * We have a problem here in that only n==M will get mapped to b which + # means the chances of getting b is much much less than getting any of + # the other values in the range. We can fix this by increasing our range + # artifically and using: + # + # n' = a + n(b-a+1)/M + * + # Now we only have a problem if n==M which would cause us to produce a + # number of b+1 which would be bad. So we bump M up by one to make sure + # this will never happen, and the final algorithm looks like this: + # + # n' = a + n(b-a+1)/(M+1) + * + * -RL + */ if (p_min && p_max) { /* implement range */ return_value->value.lval = p_min->value.lval + - (int)((double)p_max->value.lval * return_value->value.lval/(PHP_RAND_MAX+(double)p_min->value.lval)); + (int)((double)(p_max->value.lval - p_min->value.lval + 1) * return_value->value.lval/(PHP_RAND_MAX+1.0)); } } +/* }}} */ +/* {{{ proto int mt_rand([int min, int max]) + Returns a random number from Mersenne Twister */ PHP_FUNCTION(mt_rand) { pval *p_min=NULL, *p_max=NULL; @@ -304,16 +339,23 @@ if (p_min && p_max) { /* implement range */ return_value->value.lval = p_min->value.lval + - (int)((double)p_max->value.lval * return_value->value.lval/(PHP_RAND_MAX+(double)p_min->value.lval)); + (int)((double)(p_max->value.lval - p_min->value.lval + 1) * return_value->value.lval/(PHP_RAND_MAX+1.0)); } } +/* }}} */ +/* {{{ proto int getrandmax(void) + Returns the maximum value a random number can have */ PHP_FUNCTION(getrandmax) { return_value->type = IS_LONG; return_value->value.lval = PHP_RAND_MAX; } +/* }}} */ + +/* {{{ proto int mt_getrandmax(void) + Returns the maximum value a random number from Mersenne Twister can have */ PHP_FUNCTION(mt_getrandmax) { return_value->type = IS_LONG; @@ -323,7 +365,7 @@ */ return_value->value.lval = 2147483647; /* 2^^31 */ } - +/* }}} */ /* * Local variables: * tab-width: 4

« previous php.version4 (#2665) next »