CVS update: php3/functions

From: Date: Mon, 15 Feb 1999 06:04:12 +0000
Subject: CVS update: php3/functions
Groups: php.dev 
Request: Send a blank email to php-dev+get-4094@lists.php.net to get a copy of this message
Date: Monday February 15, 1999 @ 1:04 Author: rasmus Update of /repository/php3/functions In directory asf:/u/temp/cvs-serv22975/functions Modified Files: rand.c Log Message: A slight tweak Index: php3/functions/rand.c diff -c php3/functions/rand.c:1.34 php3/functions/rand.c:1.35 *** php3/functions/rand.c:1.34 Sun Feb 14 23:04:48 1999 --- php3/functions/rand.c Mon Feb 15 01:04:11 1999 *************** *** 30,36 **** | Based on code from: Shawn Cokus <Cokus@math.washington.edu> | +----------------------------------------------------------------------+ */ ! /* $Id: rand.c,v 1.34 1999/02/15 04:04:48 rasmus Exp $ */ #include <stdlib.h> --- 30,36 ---- | Based on code from: Shawn Cokus <Cokus@math.washington.edu> | +----------------------------------------------------------------------+ */ ! /* $Id: rand.c,v 1.35 1999/02/15 06:04:11 rasmus Exp $ */ #include <stdlib.h> *************** *** 290,306 **** * 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' = n(b-a+1)/M + a * ! * This equally distributes the range, but we have a problem if n==M ! * so we toss in a check for that. * * -RL */ if (p_min && p_max) { /* implement range */ ! return_value->value.lval = (return_value->value.lval==PHP_RAND_MAX) ? p_max->value.lval : ! p_min->value.lval + ! (int)((double)(p_max->value.lval - p_min->value.lval + 1) * return_value->value.lval/PHP_RAND_MAX); } } /* }}} */ --- 290,315 ---- * 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 - p_min->value.lval + 1) * return_value->value.lval/(PHP_RAND_MAX+1.0)); } } /* }}} */ *************** *** 342,350 **** /* see the comment in the php3_rand() function about this ugly algorithm */ if (p_min && p_max) { /* implement range */ ! return_value->value.lval = (return_value->value.lval==PHP_RAND_MAX) ? p_max->value.lval : ! p_min->value.lval + ! (int)((double)(p_max->value.lval - p_min->value.lval + 1) * return_value->value.lval/PHP_RAND_MAX); } } /* }}} */ --- 351,358 ---- /* see the comment in the php3_rand() function about this ugly algorithm */ if (p_min && p_max) { /* implement range */ ! return_value->value.lval = p_min->value.lval + ! (int)((double)(p_max->value.lval - p_min->value.lval + 1) * return_value->value.lval/(PHP_RAND_MAX+1.0)); } } /* }}} */ -- PHP Development Mailing List http://www.php.net/ To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net For help: php-dev-help@lists.php.net

« previous php.dev (#4094) next »