CVS update: php3/functions
| From: | rasmus | Date: | Mon, 15 Feb 1999 04:04:49 +0000 |
| Subject: | CVS update: php3/functions | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-4093@lists.php.net to get a copy of this message | ||
Date: Sunday February 14, 1999 @ 23:04
Author: rasmus
Update of /repository/php3/functions
In directory asf:/home/rasmus/php3/functions
Modified Files:
rand.c
Log Message:
Fix the broken rand() and mt_rand() ranges
Index: php3/functions/rand.c
diff -c php3/functions/rand.c:1.33 php3/functions/rand.c:1.34
*** php3/functions/rand.c:1.33 Mon Jan 4 09:25:21 1999
--- php3/functions/rand.c Sun Feb 14 23:04:48 1999
***************
*** 30,36 ****
| Based on code from: Shawn Cokus <Cokus@math.washington.edu> |
+----------------------------------------------------------------------+
*/
! /* $Id: rand.c,v 1.33 1999/01/04 14:25:21 jah Exp $ */
#include <stdlib.h>
--- 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>
***************
*** 282,291 ****
return_value->value.lval = rand();
#endif
#endif
!
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));
}
}
/* }}} */
--- 282,306 ----
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' = 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);
}
}
/* }}} */
***************
*** 325,333 ****
*/
return_value->value.lval = (long)(randomMT() >> 1);
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));
}
}
/* }}} */
--- 340,350 ----
*/
return_value->value.lval = (long)(randomMT() >> 1);
+ /* 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);
}
}
/* }}} */
--
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