Skip to content

Random Extension 5.x follow-up #9052

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
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
1 change: 1 addition & 0 deletions EXTENSIONS
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ PRIMARY MAINTAINER Go Kudo <zeriyoshi@gmail.com> (2022 - 2022)
Tim Düsterhus <timwolla@php.net> (2022 - 2022)
MAINTENANCE: Maintained
STATUS: Working
SINCE: 8.2.0
-------------------------------------------------------------------------------
EXTENSION: readline
PRIMARY MAINTAINER: Unknown
Expand Down
8 changes: 8 additions & 0 deletions UPGRADING.INTERNALS
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ PHP 8.2 INTERNALS UPGRADE NOTES
are deprecated (see main UPGRADING notes). To suppress the notice, e.g. to
avoid duplicates when processing the same value multiple times, pass or add
IS_CALLABLE_SUPPRESS_DEPRECATIONS to the check_flags parameter.
* php_lcg.h, php_rand.h, php_mt_rand.h and php_random.h has already removed in
ext/standard, They have been merged into a single ext/random/php_random.h
header file. If you are using them in an extension, change the headers to read as follows
#if PHP_VERSION_ID < 80200
# include "ext/standard/php_XYZ.h"
#else
# include "ext/random/php_random.h"
#endif

========================
2. Build system changes
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 @@ -120,7 +120,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.

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

/* {{{ Generate random number in range */
PHP_METHOD(Random_Randomizer, getInt)
/* {{{ Generate random number */
PHP_METHOD(Random_Randomizer, nextInt)
{
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_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 (randomizer->status->last_unsafe) {
zend_throw_exception(spl_ce_RuntimeException, "Random number generation failed", 0);
RETURN_THROWS();
}
RETURN_LONG((zend_long) (result >> 1));
}
if (randomizer->status->last_unsafe) {
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;

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

// nextInt
for ($i = 0; $i < 1000; $i++) {
try {
if (!\is_int($randomizer->nextInt())) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The result cannot be not int due to the return type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in new PR: #9057

die($engine::class . ': nextInt: failure');
}
} catch (\RuntimeException $e) {
if ($e->getMessage() !== 'Generated value exceeds size of int') {
throw $e;
}
}
}

// getInt
for ($i = 0; $i < 1000; $i++) {
$result = $randomizer->getInt(-50, 50);
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
12 changes: 6 additions & 6 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,8 +36,8 @@ 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}");
}
Expand Down Expand Up @@ -65,8 +65,8 @@ 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}");
}
Expand Down
1 change: 0 additions & 1 deletion ext/standard/php_lcg.h

This file was deleted.

1 change: 0 additions & 1 deletion ext/standard/php_mt_rand.h

This file was deleted.

1 change: 0 additions & 1 deletion ext/standard/php_rand.h

This file was deleted.

1 change: 0 additions & 1 deletion ext/standard/php_random.h

This file was deleted.