Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112610 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 87524 invoked from network); 23 Dec 2020 14:08:46 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Dec 2020 14:08:46 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7AA841804E4 for ; Wed, 23 Dec 2020 05:41:43 -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.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-ej1-f46.google.com (mail-ej1-f46.google.com [209.85.218.46]) (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 ; Wed, 23 Dec 2020 05:41:42 -0800 (PST) Received: by mail-ej1-f46.google.com with SMTP id ga15so22890633ejb.4 for ; Wed, 23 Dec 2020 05:41:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=Gomix6uHU2gNop7bbuagVjBkio9DFLX+S+iDrJbXRog=; b=tDL9rkJPzaQJxZ1FgUPldoXjXYp2wwoFBvdRH2BcVnUZoPVt8CI5tTgC3qYnn1wcAL 2xtTgv3KoDrxlWXgRL7DMzIdMoBYjvSVfp5XxejWWxjQOnBg/8uET4InQrce2RMPvket nnzPIo1cYHddGFM5mUNGnbgqLF0+RDcs9HUrUiyQVQVbd6NSMYyAWUim0T7ACflBDeXU 3AttVWUiQfCM0691guLvcu9VsOzveJCgPDWfL7ET20tV6W9B1aQmh3/9WXJCH6LgPyD+ G94ATD3k1siy41VoBJi8q/DxdiR93pRBFDJO/Tma4ydbptKyot6jju91fM7RABhV5crx gIEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=Gomix6uHU2gNop7bbuagVjBkio9DFLX+S+iDrJbXRog=; b=qGhzEdaxQBDmKjYjUmhChDgOSgvjo8zXNabSz0FOC2nu3pD9Jz42T9L0tQOZF9KIk0 +ARjThalhDulNZqOLeGhYq8Jk8cwQLeptjjvP/URUAaR328EqHrL2WJGggG0stb/AX7P DG/dZ3q8epQ6BySvdu+m93wSi5d0T7CxhDNeN30Zt7wgusiZ/tz1MPOQAaHcTZA3IHYO 6wF72vm0J1iuaFhGRnv9/BUx7cuIJw5lWT8+QlkDHwFSJMmvDkEFXbMXLcQT1t0mkH1B +3o1XWeA+gfb3BpQwmmjtU3RY664gft9dHOjls+iG85rFczq3sm3g/enHzL8yu4zqQEd r0+A== X-Gm-Message-State: AOAM530u8frWmgYxlYGNeXnkL6+da9GN21ToPFcewy14JwQYa+rTJ+qD v6KKfN5zz0gKWB4ValURl8zqVWEylCdDhxzUcVR3TXipeLHqjTH6 X-Google-Smtp-Source: ABdhPJyard+HOTBS84EbesikWPuNBcPb4mmJy7AtTTsrt8DJHK9TJqYHhBkuwFwGjGks7EHtNbuSoDUvUrRvZsYWMtE= X-Received: by 2002:a17:906:ce21:: with SMTP id sd1mr23533905ejb.396.1608730900178; Wed, 23 Dec 2020 05:41:40 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 23 Dec 2020 22:41:29 +0900 Message-ID: To: tyson andre , internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000d82a3905b721d9e4" Subject: Re: [PHP-DEV] Re: Improving PRNG implementation. From: zeriyoshi@gmail.com (zeriyoshi) --000000000000d82a3905b721d9e4 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 =3D null, ?int $max =3D 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 =3D 1): int|string|arr= ay; public function strShuffle(string $string): string; } ``` This can be overly structured, but it will serve all purposes. Regards, Go Kudo 2020=E5=B9=B412=E6=9C=8823=E6=97=A5(=E6=B0=B4) 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 algorith= ms. > > 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#shuf= fle(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, in= t > $max]): int {} > // If this is intended to be secure, whateverprefix_array_rand may need t= o > 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 --000000000000d82a3905b721d9e4--