HiĀ Go Kudo,

**A possible alternative that is widely used in other programming languages is 
to limit the interface API to only generating bytes/integers,**
and to provide global functions that would use generic random number generator 
objects (from internal or user-provided code) in their algorithms.

This would also make it easier to use those generators in brand new algorithms 
that weren't in the initial RFC.
(or in algorithms written by users in PHP)

This alternative is similar to Java 
https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#shuffle(java.util.List,%20java.util.Random)
and https://docs.oracle.com/javase/7/docs/api/java/util/Random.html
and C++ https://www.cplusplus.com/reference/algorithm/shuffle/
where the algorithms accept a random number generator conforming to some 
interface
and Python https://docs.python.org/3/library/random.html#random.shuffle

```
interface PRNGInterface { 
    public function random[Signed]Int(): int;  // between PHP_INT_MIN and 
PHP_INT_MAX (4 bytes or 8 bytes)
    public function randomBytes(int $length): string // $length bytes of raw 
data
    // possibly randomByte(): int // between 0 and 255
    // public function randomIntInRange(int $min, int $max)
    // __serialize(), __unserialize() may be provided, but may be 
counterproductive for classes that wrap /dev/urandom or random_bytes()?
}
// possibly provide a trait that provides defaults based on abstract function 
randomInt

function whateverprefixornamespace_rand(PRNGInterface $rng, [int $min, int 
$max]): int {}
// If this is intended to be secure, whateverprefix_array_rand may need to 
avoid the optimizations used by array_rand for sparse arrays
function whateverprefixornamespace_array_rand(PRNGInterface $rng, array $array, 
...): int {}
function whateverprefixornamespace_shuffle(PRNGInterface $rng, array &$array): 
bool;
// alternately might be possible by extending existing global functions with an 
optional parameter
```

Thanks,
- Tyson
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to