Skip to content

[random] split getInt() with no arguments into nextInt() #9057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ PHP NEWS
call twice) (zeriyoshi)
. Change Mt19937 to throw a ValueError instead of InvalidArgumentException
for invalid $mode. (timwolla)
. Splitting Random\Randomizer::getInt() (without arguments) to
Random\Randomizer::nextInt(). (zeriyoshi)

- Sockets:
. Added SOL_FILTER socket option for Solaris. (David Carlier)
Expand Down
4 changes: 3 additions & 1 deletion ext/random/random.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ final class Randomizer

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

public function getInt(int $min = UNKNOWN, int $max = UNKNOWN): int {}
public function nextInt(): int {}

public function getInt(int $min, int $max): int {}

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

Expand Down
8 changes: 6 additions & 2 deletions ext/random/random_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 22 additions & 14 deletions ext/random/randomizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,26 +91,34 @@ PHP_METHOD(Random_Randomizer, __construct)
}
/* }}} */

/* {{{ Generate positive random number */
PHP_METHOD(Random_Randomizer, nextInt)
{
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
uint64_t result;

ZEND_PARSE_PARAMETERS_NONE();

result = randomizer->algo->generate(randomizer->status);
if (randomizer->status->last_generated_size > sizeof(zend_long)) {
zend_throw_exception(spl_ce_RuntimeException, "Generated value exceeds size of int", 0);
RETURN_THROWS();
}
if (EG(exception)) {
zend_throw_exception(spl_ce_RuntimeException, "Random number generation failed", 0);
RETURN_THROWS();
}

RETURN_LONG((zend_long) (result >> 1));
}
/* }}} */

/* {{{ Generate random number in range */
PHP_METHOD(Random_Randomizer, getInt)
{
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
uint64_t result;
zend_long min, max;
int argc = ZEND_NUM_ARGS();

if (argc == 0) {
result = randomizer->algo->generate(randomizer->status);
if (randomizer->status->last_generated_size > sizeof(zend_long)) {
zend_throw_exception(spl_ce_RuntimeException, "Generated value exceeds size of int", 0);
RETURN_THROWS();
}
if (EG(exception)) {
zend_throw_exception(spl_ce_RuntimeException, "Random number generation failed", 0);
RETURN_THROWS();
}
RETURN_LONG((zend_long) (result >> 1));
}

ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_LONG(min)
Expand Down
17 changes: 14 additions & 3 deletions ext/random/tests/03_randomizer/basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ $engines[] = new UserEngine();
foreach ($engines as $engine) {
$randomizer = new Random\Randomizer($engine);

// nextInt
for ($i = 0; $i < 1000; $i++) {
try {
$randomizer->nextInt();
} catch (\RuntimeException $e) {
if ($e->getMessage() !== 'Generated value exceeds size of int') {
die($engine::class . ": nextInt: failure: {$e->getMessage()}");
}
}
}

// getInt
for ($i = 0; $i < 1000; $i++) {
$result = $randomizer->getInt(-50, 50);
Expand All @@ -39,7 +50,7 @@ foreach ($engines as $engine) {
for ($i = 0; $i < 1000; $i++) {
$length = \random_int(1, 1024);
if (\strlen($randomizer->getBytes($length)) !== $length) {
die($engine::class . ': getBytes: failure.');
die($engine::class . ': getBytes: failure');
}
}

Expand All @@ -53,14 +64,14 @@ foreach ($engines as $engine) {
}
}

die($engine::class . ': shuffleArray: failure.');
die($engine::class . ': shuffleArray: failure');
})();

// shuffleBytes
$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.';
$shuffled_string = $randomizer->shuffleBytes($string);
if ($string === $shuffled_string) {
die($engine::class . ': shuffleBytes: failure.');
die($engine::class . ': shuffleBytes: failure');
}
}

Expand Down
4 changes: 2 additions & 2 deletions ext/random/tests/03_randomizer/compatibility_mt.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ Random: Randomizer: Compatibility: Mt19937
$randomizer = new \Random\Randomizer(new \Random\Engine\Mt19937(1234, \MT_RAND_PHP));
\mt_srand(1234, \MT_RAND_PHP);
for ($i = 0; $i < 1000; $i++) {
if ($randomizer->getInt() !== \mt_rand()) {
if ($randomizer->nextInt() !== \mt_rand()) {
die('failure');
}
}

$randomizer = new \Random\Randomizer(new \Random\Engine\Mt19937(1234, \MT_RAND_MT19937));
\mt_srand(1234, \MT_RAND_MT19937);
for ($i = 0; $i < 1000; $i++) {
if ($randomizer->getInt() !== \mt_rand()) {
if ($randomizer->nextInt() !== \mt_rand()) {
die('failure');
}
}
Expand Down
18 changes: 6 additions & 12 deletions ext/random/tests/03_randomizer/compatibility_user.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ $user_randomizer = new \Random\Randomizer(new class () implements \Random\Engine
}
});
for ($i = 0; $i < 1000; $i++) {
$native = $native_randomizer->getInt();
$user = $user_randomizer->getInt();
$native = $native_randomizer->nextInt();
$user = $user_randomizer->nextInt();
if ($native !== $user) {
die("failure Mt19937 i: {$i} native: {$native} user: {$user}");
}
Expand All @@ -36,16 +36,13 @@ try {
});

for ($i = 0; $i < 1000; $i++) {
$native = $native_randomizer->getInt();
$user = $user_randomizer->getInt();
$native = $native_randomizer->nextInt();
$user = $user_randomizer->nextInt();
if ($native !== $user) {
die("failure PcgOneseq128XslRr64 i: {$i} native: {$native} user: {$user}");
}
}
} catch (\RuntimeException $e) {
if (\PHP_INT_SIZE >= 8) {
throw $e;
}
if ($e->getMessage() !== 'Generated value exceeds size of int') {
throw $e;
}
Expand All @@ -65,16 +62,13 @@ try {
});

for ($i = 0; $i < 1000; $i++) {
$native = $native_randomizer->getInt();
$user = $user_randomizer->getInt();
$native = $native_randomizer->nextInt();
$user = $user_randomizer->nextInt();
if ($native !== $user) {
die("failure Xoshiro256StarStar i: {$i} native: {$native} user: {$user}");
}
}
} catch (\RuntimeException $e) {
if (\PHP_INT_SIZE >= 8) {
throw $e;
}
if ($e->getMessage() !== 'Generated value exceeds size of int') {
throw $e;
}
Expand Down