PHP_FE(proc_nice, arginfo_proc_nice)
#endif
- PHP_FALIAS(rand, mt_rand, arginfo_mt_rand)
+ PHP_FE(rand, arginfo_mt_rand)
PHP_FALIAS(srand, mt_srand, arginfo_mt_srand)
PHP_FALIAS(getrandmax, mt_getrandmax, arginfo_mt_getrandmax)
PHP_FE(mt_rand, arginfo_mt_rand)
}
/* }}} */
+/* {{{ php_mt_rand_common
+ * rand() allows min > max, mt_rand does not */
+PHPAPI zend_long php_mt_rand_common(zend_long min, zend_long max)
+{
+ zend_long n;
+
+ if (BG(mt_rand_mode) == MT_RAND_MT19937) {
+ return 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 n;
+}
+/* }}} */
+
/* {{{ proto int mt_rand([int min, int max])
Returns a random number from Mersenne Twister */
PHP_FUNCTION(mt_rand)
{
zend_long min;
zend_long max;
- zend_long n;
int argc = ZEND_NUM_ARGS();
if (argc == 0) {
if (UNEXPECTED(max < min)) {
php_error_docref(NULL, E_WARNING, "max(" ZEND_LONG_FMT ") is smaller than min(" ZEND_LONG_FMT ")", max, min);
- max ^= min ^= max ^= min;
+ RETURN_FALSE;
}
- 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);
+ RETURN_LONG(php_mt_rand_common(min, max));
}
/* }}} */
PHPAPI void php_mt_srand(uint32_t seed);
PHPAPI uint32_t php_mt_rand(void);
PHPAPI zend_long php_mt_rand_range(zend_long min, zend_long max);
+PHPAPI zend_long php_mt_rand_common(zend_long min, zend_long max);
PHP_MINIT_FUNCTION(mt_rand);
}
/* }}} */
+/* {{{ proto int mt_rand([int min, int max])
+ Returns a random number from Mersenne Twister */
+PHP_FUNCTION(rand)
+{
+ zend_long min;
+ zend_long max;
+ int argc = ZEND_NUM_ARGS();
+
+ if (argc == 0) {
+ RETURN_LONG(php_mt_rand() >> 1);
+ }
+
+ if (zend_parse_parameters(argc, "ll", &min, &max) == FAILURE) {
+ return;
+ }
+
+ if (max < min) {
+ RETURN_LONG(php_mt_rand_common(max, min));
+ }
+
+ RETURN_LONG(php_mt_rand_common(min, max));
+}
+/* }}} */
+
/*
* Local variables:
* tab-width: 4