On Wed, May 12, 2021 at 6:43 PM Christian Schneider <cschn...@cschneid.com> wrote:
> Am 11.05.2021 um 23:20 schrieb Larry Garfield <la...@garfieldtech.com>: > > 3) Perhaps it should be named RandomSequence or similar? It's not truly > generating random numbers. It's generating a repeatable but difficult to > produce sequence off of a seed value. That's not the same thing as > random_int() et al. > > > > So, at first pass, an API could be as simple as (pseudocode): > > > > class RandomSequence { > > public function __construct(int $seed = time(), $algorithm = > 'some_default') { ... } > > > > public function next(): int { ... } > > } > > > > And that's it. That's nice and simple and predictable to use. > > > Would the generated values fill the whole int range, i.e. 64 bit on modern > systems? > > And would it make sense to add max and min for the generated values like > we have with random_int()? > Because mapping arbitrary int values to a specific range is error-prone: > Just using modulo leads to bias in the resulting values. > > What I initially proposed is an interface something like: interface RandomSource { public function next($length): string; } where the string returned would be of the specified length and made up of random bytes. I understood that internally, an 32/64 bit int is equivalent to 4 or 8 bytes and that various internal implementations already use ints and not bytes to provide other constructs like shuffle() and array_rand() with random behavior. So I can understand if the function would return an int or an array of ints. Maybe you can contradict me if I understood this wrong. But to fulfill all the need for random, one can further use a wrapper class that can be called RandomGenerator that takes a RandomSource as parameter and have all types of handy methods: generateInt($min = 0, $max = PHP_INT_MAX) generateBoolean() generateNumber() // float between 0 and 1 generateString($length, $characters = '') RandomGenerator would take care of the uniform distribution of values generated based on uniform distribution of source bytes/ints generated. I think that keeping a simple way to generate random information is important so that it can easily be reimplemented in various ways when needed. I like the byte approach more because generateString(4, 'abcd') could consume only 1 byte from the random source. Of course, in this picture, RandomSource implementations can be serializable, if it makes sense. BTW, this name proposal is inspired from this library: https://github.com/paragonie/RandomLib as I'm not good with namings either. Alex