Skip to content

Commit 76e1e1b

Browse files
committed
random: split Randomizer::getInt() without argument to Randomizer::nextInt()
Since argument overloading is not safe for reflection, the method needed to be split appropriately.
1 parent fc394b4 commit 76e1e1b

File tree

6 files changed

+51
-25
lines changed

6 files changed

+51
-25
lines changed

ext/random/random.stub.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ final class Randomizer
131131

132132
public function __construct(?Engine $engine = null) {}
133133

134-
public function getInt(int $min = UNKNOWN, int $max = UNKNOWN): int {}
134+
public function nextInt(): int {}
135+
136+
public function getInt(int $min, int $max): int {}
135137

136138
public function getBytes(int $length): string {}
137139

ext/random/random_arginfo.h

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/random/randomizer.c

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,26 +91,34 @@ PHP_METHOD(Random_Randomizer, __construct)
9191
}
9292
/* }}} */
9393

94+
/* {{{ Generate random number */
95+
PHP_METHOD(Random_Randomizer, nextInt)
96+
{
97+
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
98+
uint64_t result;
99+
100+
ZEND_PARSE_PARAMETERS_NONE();
101+
102+
result = randomizer->algo->generate(randomizer->status);
103+
if (randomizer->status->last_generated_size > sizeof(zend_long)) {
104+
zend_throw_exception(spl_ce_RuntimeException, "Generated value exceeds size of int", 0);
105+
RETURN_THROWS();
106+
}
107+
if (EG(exception)) {
108+
zend_throw_exception(spl_ce_RuntimeException, "Random number generation failed", 0);
109+
RETURN_THROWS();
110+
}
111+
112+
RETURN_LONG((zend_long) (result >> 1));
113+
}
114+
/* }}} */
115+
94116
/* {{{ Generate random number in range */
95117
PHP_METHOD(Random_Randomizer, getInt)
96118
{
97119
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
98120
uint64_t result;
99121
zend_long min, max;
100-
int argc = ZEND_NUM_ARGS();
101-
102-
if (argc == 0) {
103-
result = randomizer->algo->generate(randomizer->status);
104-
if (randomizer->status->last_generated_size > sizeof(zend_long)) {
105-
zend_throw_exception(spl_ce_RuntimeException, "Generated value exceeds size of int", 0);
106-
RETURN_THROWS();
107-
}
108-
if (EG(exception)) {
109-
zend_throw_exception(spl_ce_RuntimeException, "Random number generation failed", 0);
110-
RETURN_THROWS();
111-
}
112-
RETURN_LONG((zend_long) (result >> 1));
113-
}
114122

115123
ZEND_PARSE_PARAMETERS_START(2, 2)
116124
Z_PARAM_LONG(min)

ext/random/tests/03_randomizer/basic.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ $engines[] = new UserEngine();
2727
foreach ($engines as $engine) {
2828
$randomizer = new Random\Randomizer($engine);
2929

30+
// nextInt
31+
for ($i = 0; $i < 1000; $i++) {
32+
try {
33+
$randomizer->nextInt();
34+
} catch (\RuntimeException $e) {
35+
if ($e->getMessage() !== 'Generated value exceeds size of int') {
36+
die($engine::class . ': nextInt: failure: {$e->getMesasge()}');
37+
throw $e;
38+
}
39+
}
40+
}
41+
3042
// getInt
3143
for ($i = 0; $i < 1000; $i++) {
3244
$result = $randomizer->getInt(-50, 50);

ext/random/tests/03_randomizer/compatibility_mt.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ Random: Randomizer: Compatibility: Mt19937
66
$randomizer = new \Random\Randomizer(new \Random\Engine\Mt19937(1234, \MT_RAND_PHP));
77
\mt_srand(1234, \MT_RAND_PHP);
88
for ($i = 0; $i < 1000; $i++) {
9-
if ($randomizer->getInt() !== \mt_rand()) {
9+
if ($randomizer->nextInt() !== \mt_rand()) {
1010
die('failure');
1111
}
1212
}
1313

1414
$randomizer = new \Random\Randomizer(new \Random\Engine\Mt19937(1234, \MT_RAND_MT19937));
1515
\mt_srand(1234, \MT_RAND_MT19937);
1616
for ($i = 0; $i < 1000; $i++) {
17-
if ($randomizer->getInt() !== \mt_rand()) {
17+
if ($randomizer->nextInt() !== \mt_rand()) {
1818
die('failure');
1919
}
2020
}

ext/random/tests/03_randomizer/compatibility_user.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ $user_randomizer = new \Random\Randomizer(new class () implements \Random\Engine
1515
}
1616
});
1717
for ($i = 0; $i < 1000; $i++) {
18-
$native = $native_randomizer->getInt();
19-
$user = $user_randomizer->getInt();
18+
$native = $native_randomizer->nextInt();
19+
$user = $user_randomizer->nextInt();
2020
if ($native !== $user) {
2121
die("failure Mt19937 i: {$i} native: {$native} user: {$user}");
2222
}
@@ -36,8 +36,8 @@ try {
3636
});
3737

3838
for ($i = 0; $i < 1000; $i++) {
39-
$native = $native_randomizer->getInt();
40-
$user = $user_randomizer->getInt();
39+
$native = $native_randomizer->nextInt();
40+
$user = $user_randomizer->nextInt();
4141
if ($native !== $user) {
4242
die("failure PcgOneseq128XslRr64 i: {$i} native: {$native} user: {$user}");
4343
}
@@ -65,8 +65,8 @@ try {
6565
});
6666

6767
for ($i = 0; $i < 1000; $i++) {
68-
$native = $native_randomizer->getInt();
69-
$user = $user_randomizer->getInt();
68+
$native = $native_randomizer->nextInt();
69+
$user = $user_randomizer->nextInt();
7070
if ($native !== $user) {
7171
die("failure Xoshiro256StarStar i: {$i} native: {$native} user: {$user}");
7272
}

0 commit comments

Comments
 (0)