Skip to content

Commit 4e92c74

Browse files
zeriyoshicmb69
authored andcommitted
random: split Randomizer::getInt() without argument to Randomizer::nextInt()
Since argument overloading is not safe for reflection, the method needed to be split appropriately. Co-authored-by: Tim Düsterhus <timwolla@googlemail.com> Closes GH-9057.
1 parent 59d257d commit 4e92c74

File tree

7 files changed

+55
-34
lines changed

7 files changed

+55
-34
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ PHP NEWS
4141
call twice) (zeriyoshi)
4242
. Change Mt19937 to throw a ValueError instead of InvalidArgumentException
4343
for invalid $mode. (timwolla)
44+
. Splitted Random\Randomizer::getInt() (without arguments) to
45+
Random\Randomizer::nextInt(). (zeriyoshi)
4446

4547
- Sockets:
4648
. Added SOL_FILTER socket option for Solaris. (David Carlier)

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 positive 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: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ $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->getMessage()}");
37+
}
38+
}
39+
}
40+
3041
// getInt
3142
for ($i = 0; $i < 1000; $i++) {
3243
$result = $randomizer->getInt(-50, 50);
@@ -39,7 +50,7 @@ foreach ($engines as $engine) {
3950
for ($i = 0; $i < 1000; $i++) {
4051
$length = \random_int(1, 1024);
4152
if (\strlen($randomizer->getBytes($length)) !== $length) {
42-
die($engine::class . ': getBytes: failure.');
53+
die($engine::class . ': getBytes: failure');
4354
}
4455
}
4556

@@ -53,14 +64,14 @@ foreach ($engines as $engine) {
5364
}
5465
}
5566

56-
die($engine::class . ': shuffleArray: failure.');
67+
die($engine::class . ': shuffleArray: failure');
5768
})();
5869

5970
// shuffleBytes
6071
$string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
6172
$shuffled_string = $randomizer->shuffleBytes($string);
6273
if ($string === $shuffled_string) {
63-
die($engine::class . ': shuffleBytes: failure.');
74+
die($engine::class . ': shuffleBytes: failure');
6475
}
6576
}
6677

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 & 12 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,16 +36,13 @@ 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
}
4444
}
4545
} catch (\RuntimeException $e) {
46-
if (\PHP_INT_SIZE >= 8) {
47-
throw $e;
48-
}
4946
if ($e->getMessage() !== 'Generated value exceeds size of int') {
5047
throw $e;
5148
}
@@ -65,16 +62,13 @@ try {
6562
});
6663

6764
for ($i = 0; $i < 1000; $i++) {
68-
$native = $native_randomizer->getInt();
69-
$user = $user_randomizer->getInt();
65+
$native = $native_randomizer->nextInt();
66+
$user = $user_randomizer->nextInt();
7067
if ($native !== $user) {
7168
die("failure Xoshiro256StarStar i: {$i} native: {$native} user: {$user}");
7269
}
7370
}
7471
} catch (\RuntimeException $e) {
75-
if (\PHP_INT_SIZE >= 8) {
76-
throw $e;
77-
}
7872
if ($e->getMessage() !== 'Generated value exceeds size of int') {
7973
throw $e;
8074
}

0 commit comments

Comments
 (0)