Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112632 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 98817 invoked from network); 29 Dec 2020 09:51:45 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 29 Dec 2020 09:51:45 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id F1F291804C4 for ; Tue, 29 Dec 2020 01:26:06 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from wp160.webpack.hosteurope.de (wp160.webpack.hosteurope.de [80.237.132.167]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 29 Dec 2020 01:26:06 -0800 (PST) Received: from [2a02:8109:9d40:1d44:40b6:f5d3:c567:d46c] (helo=nas.fritz.box); authenticated by wp160.webpack.hosteurope.de running ExIM with esmtpsa (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) id 1kuBGW-0000rf-2q; Tue, 29 Dec 2020 10:26:04 +0100 To: zeriyoshi , tyson andre , internals@lists.php.net References: Message-ID: Date: Tue, 29 Dec 2020 10:26:03 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------9DD3435547EAE980FE740059" Content-Language: en-US X-bounce-key: webpack.hosteurope.de;marc@mabe.berlin;1609233966;b1d66489; X-HE-SMSGID: 1kuBGW-0000rf-2q Subject: Re: [PHP-DEV] Re: Improving PRNG implementation. From: marc@mabe.berlin (Marc) --------------9DD3435547EAE980FE740059 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit 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 initial 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 : > >> 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 --------------9DD3435547EAE980FE740059--