zend_ulong limit;
zend_ulong result;
-#if ZEND_ULONG_MAX > UINT32_MAX
- result = ((zend_ulong)php_mt_rand() << 32) | php_mt_rand();
-#else
result = php_mt_rand();
-#endif
+ if (umax > UINT32_MAX) {
+ result = (result << 32) | php_mt_rand();
+ }
/* Special case where no modulus is required */
if (UNEXPECTED(umax == ZEND_ULONG_MAX)) {
/* Discard numbers over the limit to avoid modulo bias */
while (UNEXPECTED(result > limit)) {
-#if ZEND_ULONG_MAX > UINT32_MAX
result = (result << 32) | php_mt_rand();
-#else
- result = php_mt_rand();
-#endif
}
}
{
zend_long min;
zend_long max;
+ zend_long n;
int argc = ZEND_NUM_ARGS();
if (argc == 0) {
RETURN_FALSE;
}
- RETURN_LONG(php_mt_rand_range(min, max));
+ if (BG(mt_rand_mode) == MT_RAND_MT19937) {
+ RETURN_LONG(php_mt_rand_range(min, max));
+ }
+
+ /* Legacy mode deliberately not inside php_mt_rand_range()
+ * to prevent other functions being affected */
+ n = (zend_long)php_mt_rand() >> 1;
+ RAND_RANGE_BADSCALING(n, min, max, PHP_MT_RAND_MAX);
+ RETURN_LONG(n);
}
/* }}} */
*
* -RL
*/
+#define RAND_RANGE_BADSCALING(__n, __min, __max, __tmax) \
+ (__n) = (__min) + (zend_long) ((double) ( (double) (__max) - (__min) + 1.0) * ((__n) / ((__tmax) + 1.0)))
+
#define RAND_RANGE(__n, __min, __max, __tmax) \
(__n) = php_mt_rand_range((__min), (__max))