Skip to content

Commit 69e7d8d

Browse files
committed
Make mode selection part of mt_srand()
The mode of operation is intrinsically linked to seeding, so this makes a lot of sense
1 parent bb1f822 commit 69e7d8d

6 files changed

Lines changed: 17 additions & 34 deletions

File tree

‎ext/standard/basic_functions.c‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,7 @@ ZEND_END_ARG_INFO()
18861886
/* {{{ mt_rand.c */
18871887
ZEND_BEGIN_ARG_INFO_EX(arginfo_mt_srand, 0, 0, 0)
18881888
ZEND_ARG_INFO(0, seed)
1889+
ZEND_ARG_INFO(0, mode)
18891890
ZEND_END_ARG_INFO()
18901891

18911892
ZEND_BEGIN_ARG_INFO_EX(arginfo_mt_rand, 0, 0, 0)
@@ -1895,10 +1896,6 @@ ZEND_END_ARG_INFO()
18951896

18961897
ZEND_BEGIN_ARG_INFO(arginfo_mt_getrandmax, 0)
18971898
ZEND_END_ARG_INFO()
1898-
1899-
ZEND_BEGIN_ARG_INFO_EX(arginfo_mt_rand_mode, 0, 0, 1)
1900-
ZEND_ARG_INFO(0, mode)
1901-
ZEND_END_ARG_INFO()
19021899
/* }}} */
19031900
/* {{{ random.c */
19041901
ZEND_BEGIN_ARG_INFO_EX(arginfo_random_bytes, 0, 0, 0)
@@ -2858,7 +2855,6 @@ const zend_function_entry basic_functions[] = { /* {{{ */
28582855
PHP_FE(mt_rand, arginfo_mt_rand)
28592856
PHP_FE(mt_srand, arginfo_mt_srand)
28602857
PHP_FE(mt_getrandmax, arginfo_mt_getrandmax)
2861-
PHP_FE(mt_rand_mode, arginfo_mt_rand_mode)
28622858

28632859
PHP_FE(random_bytes, arginfo_random_bytes)
28642860
PHP_FE(random_int, arginfo_random_int)

‎ext/standard/mt_rand.c‎

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,22 @@ PHPAPI uint32_t php_mt_rand(void)
189189
PHP_FUNCTION(mt_srand)
190190
{
191191
zend_long seed = 0;
192+
zend_long mode = MT_RAND_MT19937;
192193

193-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &seed) == FAILURE)
194+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ll", &seed, &mode) == FAILURE)
194195
return;
195196

196197
if (ZEND_NUM_ARGS() == 0)
197198
seed = GENERATE_SEED();
198199

200+
switch (mode) {
201+
case MT_RAND_PHP:
202+
BG(mt_rand_mode) = MT_RAND_PHP;
203+
break;
204+
default:
205+
BG(mt_rand_mode) = MT_RAND_MT19937;
206+
}
207+
199208
php_mt_srand(seed);
200209
}
201210
/* }}} */
@@ -283,25 +292,6 @@ PHP_FUNCTION(mt_getrandmax)
283292
}
284293
/* }}} */
285294

286-
/* {{{ proto int mt_rand_mode(int)
287-
Switch mt_rand between standard and legacy modes */
288-
PHP_FUNCTION(mt_rand_mode)
289-
{
290-
zend_long mode;
291-
292-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &mode) == FAILURE) {
293-
return;
294-
}
295-
296-
switch (mode) {
297-
case MT_RAND_PHP:
298-
BG(mt_rand_mode) = MT_RAND_PHP;
299-
break;
300-
default:
301-
BG(mt_rand_mode) = MT_RAND_MT19937;
302-
}
303-
}
304-
/* }}} */
305295
PHP_MINIT_FUNCTION(mt_rand)
306296
{
307297
REGISTER_LONG_CONSTANT("MT_RAND_MT19937", MT_RAND_MT19937, CONST_CS | CONST_PERSISTENT);

‎ext/standard/php_math.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ PHP_FUNCTION(getrandmax);
5151
PHP_FUNCTION(mt_srand);
5252
PHP_FUNCTION(mt_rand);
5353
PHP_FUNCTION(mt_getrandmax);
54-
PHP_FUNCTION(mt_rand_mode);
5554
PHP_FUNCTION(abs);
5655
PHP_FUNCTION(ceil);
5756
PHP_FUNCTION(floor);

‎ext/standard/tests/math/mt_rand_value.phpt‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ Test mt_rand() output
33
--FILE--
44
<?php
55

6-
mt_rand_mode(MT_RAND_PHP);
7-
mt_srand(12345678);
6+
mt_srand(12345678, MT_RAND_PHP);
87

98
for ($i=0; $i<16; $i++) {
109
echo mt_rand().PHP_EOL;
@@ -18,8 +17,7 @@ for ($i=0; $i<1024; $i++) {
1817
echo $x.PHP_EOL;
1918
echo PHP_EOL;
2019

21-
mt_rand_mode(MT_RAND_MT19937);
22-
mt_srand(12345678);
20+
mt_srand(12345678 /*, MT_RAND_MT19937 */);
2321

2422
for ($i=0; $i<16; $i++) {
2523
echo mt_rand().PHP_EOL;

‎ext/standard/tests/math/mt_srand_error.phpt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
Test mt_srand() - wrong params test mt_srand()
33
--FILE--
44
<?php
5-
var_dump(mt_srand(500, true));
5+
var_dump(mt_srand(500, 0, true));
66
var_dump(mt_srand("fivehundred"));
77
var_dump(mt_srand("500ABC"));
88
?>
99
--EXPECTF--
10-
Warning: mt_srand() expects at most 1 parameter, 2 given in %s on line 2
10+
Warning: mt_srand() expects at most 2 parameters, 3 given in %s on line 2
1111
NULL
1212

1313
Warning: mt_srand() expects parameter 1 to be integer, string given in %s on line 3

‎ext/standard/tests/math/srand_error.phpt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ Test srand() function : error conditions - incorrect number of args
1313

1414
echo "*** Testing srand() : error conditions ***\n";
1515

16-
var_dump(srand(500, true));
16+
var_dump(srand(500, 0, true));
1717
var_dump(srand("fivehundred"));
1818
var_dump(srand("500ABC"));
1919
?>
2020
===Done===
2121
--EXPECTF--
2222
*** Testing srand() : error conditions ***
2323

24-
Warning: srand() expects at most 1 parameter, 2 given in %s on line %d
24+
Warning: srand() expects at most 2 parameters, 3 given in %s on line %d
2525
NULL
2626

2727
Warning: srand() expects parameter 1 to be integer, string given in %s on line %d

0 commit comments

Comments
 (0)