On 01.01.21 20:22, Go Kudo wrote: > Hi Marc, and sorry for the late reply. Email has been marked as spam... > >> I would expect the Random number generator to implement from Iterator > using an integer for current() value >> and providing utility functions (simple functions or methods of another > class) for shuffle, random array entry like this: > > Thank you very much. This suggestion makes sense and looks very structured. > > However, I feel that in the PHP world, this structuring is a bit too much. > Ease of use is very important in PHP.
One important reason for a PRNGInterface is (at least how I understand it) to allow implementing PRNG algorithms in userland but there would be no performant way to use this for shuffle and friends that's why I think such functions should consume any PRNGInterface instead of building it directly into the PRNG class. -> If you really want these as PRNG methods - one possibility could be a Trait providing these methods but this is very exotic for core functionalities. Another thing with the proposed `next*` methods is * How to get the current value? retrieving the next value only seems to be out of sync with the current PHP way (Iterator, Generator) * `nextByte(int $length)` How much values does this consume? How much random bytes can be generated with one value (4 bytes, 8 bytes, platform dependent). Does this consume multiple values to generate 1 byte 4/8 times? * same for `nextDouble()` on 32 bit platforms * Btw. it should be called `nextFloat()` as there is no double type in PHP and the float type is equivalent to C double precision > > Nevertheless, I am satisfied with this proposal. I'd like to hear from > someone who is more familiar with the PHP context about the form of the > implementation. > > But, it may be faster to actually hold a vote to ask this question. I'm not > sure how much support this proposal has at this point, but do you think > it's worth a try? I'm don't have voting rights and I'm not very active in discussions so I would be better to get some reasonable thoughts from someone more involved. Cheers Marc > > > 2020年12月29日(火) 18:26 Marc <marc@mabe.berlin>: > >> Hi zeriyoshi, >> On 23.12.20 14:41, zeriyoshi wrote: >> >> Thanks tyson. >> >> >> This would also make it easier to use those generators in brand new >> >> algorithms that weren't in the ionitial RFC. >> >> (or in algorithms written by users in PHP) >> >> This suggestion seems to make sense. Maybe the RNG should only focus on >> generating random numbers and not anything else. >> However, the fact that array and string manipulation functions are no >> longer native to C may have a speed disadvantage. >> >> So I came up with the idea of minimizing the interface definition as RNG. >> >> ``` >> interface PRNGInterface >> { >> public function nextInt(?int $min = null, ?int $max = null): int; >> public function nextDouble(): double; // maybe, non-needed. >> public function nextByte(int $length): string; >> } >> ``` >> >> The methods for array and string operations are defined separately as >> interfaces that inherit from the interface. >> >> ``` >> interface RandomInterface extends PRNGInterface >> { >> public function shuffle(array &$array): bool; >> public function arrayRand(array $array, int $num = 1): int|string|array; >> public function strShuffle(string $string): string; >> } >> ``` >> >> This can be overly structured, but it will serve all purposes. >> >> Personally I feel the interfaces still looking a bit off to me. >> >> I would expect the Random number generator to implement from Iterator >> using an integer for `current()` value >> >> and providing utility functions (simple functions or methods of another >> class) for shuffle, random array entry like this: >> >> ```php >> >> interface RNG extends Iterator { >> public function rewind(); >> public function next(); >> public function current(): int; >> public function key(): int; >> public function valid(); >> } >> >> >> interface PRNG extends RNG { >> public function __current(int $seed); >> public function getSeed(): int; >> } >> >> class RNGUtil { >> public static function shuffleArray(int $randomNumber, array $arr): >> array; >> public static function randomArrayElement(int $randomNumber, array >> $arr): mixed; >> public static function between(int $randomNumber, int $min = >> PHP_INT_MIN, int $max = PHP_INT_MAX): int; >> public static function bytes(RNG $rng, int $length): string; >> } >> >> ``` >> >> >> Regards, >> Go Kudo >> >> >> 2020年12月23日(水) 0:40 tyson andre <tysonandre...@hotmail.com> >> <tysonandre...@hotmail.com>: >> >> >> 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 >> Javahttps://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