Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114854 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 15030 invoked from network); 14 Jun 2021 09:15:16 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 14 Jun 2021 09:15:16 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 54781180543 for ; Mon, 14 Jun 2021 02:31:30 -0700 (PDT) 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_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f51.google.com (mail-lf1-f51.google.com [209.85.167.51]) (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 ; Mon, 14 Jun 2021 02:31:29 -0700 (PDT) Received: by mail-lf1-f51.google.com with SMTP id v22so20060075lfa.3 for ; Mon, 14 Jun 2021 02:31:29 -0700 (PDT) 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 :cc; bh=wh6Q3p3x8tvmbrdvEBZLlX7yE0MtYwdvyOjs9laV+p0=; b=U2DDwkqns00cDS2j3VZv8GrYwjjKV93CXYUoHbKbvI+beRca8fIFo7nAgvr+15XaT5 i50ATeemLyeU1IeF/B7ztYBDle5OsYDj8NOmnwKc9XS4gQR6YR3JzktnWhrZVBEyJQU9 0W+zqqHHdx7yq8hJsTGFJyjw810UJvM5OsCfGpw129EQm0TOnIc5nzbEorn+NKPITFUV tWowQ8PCCi27hji3uRW9oN/9A0wc1LQYy6E6nKsx8MCGzf2aoiAbBGnDxldfIaaj0HMS B8rpP0Ed0qJsq2vEb1EAs39Nzwg69+YxBoDwWAd/TAY4fsmGii11NkQ5gora1ihkG09q lywA== 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:cc; bh=wh6Q3p3x8tvmbrdvEBZLlX7yE0MtYwdvyOjs9laV+p0=; b=RpSRjMcLKRLvd0/Q8eCWT2L5u1NeEX1AePzpbdUPZmB2a7/+Wd5T3vnB17Ns8Nqfxv +LXFNcSTrNpY7I0Pltxd7g7SuBAUtRLmwNBRI9tXmqtyglNuqRxAVw3Hm7TGBLfg2x9b d6EU1hdZRtY1Hs5pos60hMaIzcXz3dtJ3R2tqEKbsRLoI0Os2JU9xqwOm6W0zWbNNLT6 1KEb1gFQbN4quW7Gsair8nmoUV3FUVtzXJgT3u7OMzMhFwvU8TPp6Nyz+Y7OlBuME6u7 U0tyuyav9wF6VIv1LYXsSLFoKNgpfR4kTzLezbV688VITKbrW/X43yU2yjWI6SjNMBer JfFQ== X-Gm-Message-State: AOAM533YdXWDEspvfJs3jjK5EG55JmGGQLjQaVh6xfyT1KOEoLsDvSoe FPx0BIb4ys/nGD0aBj8sgnsQmj7YWLfkpLkLIg== X-Google-Smtp-Source: ABdhPJwn3sEPSdfGCe47/RMBqLuFhAqSMSvg03Wn7FRjuGqw7XuPJzMeLAF0vT2rYR2luJMdgkIjW/tKD03iGl3d/+8= X-Received: by 2002:ac2:508f:: with SMTP id f15mr11382023lfm.119.1623663088189; Mon, 14 Jun 2021 02:31:28 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 14 Jun 2021 11:31:18 +0200 Message-ID: To: Go Kudo Cc: PHP internals Content-Type: multipart/alternative; boundary="0000000000009b44a405c4b685e3" Subject: Re: [PHP-DEV] Re: [RFC] Under Discussion: Add Random class and RandomNumberGenerator interface From: guilliam.xavier@gmail.com (Guilliam Xavier) --0000000000009b44a405c4b685e3 Content-Type: text/plain; charset="UTF-8" > Indeed, the current implementation appears to have several problems. So, > how about the following implementation? > > ```php > interface RandomNumberGenerator > { > public function generate(): int; > } > > final class Random > { > private RandomNumberGenerator $rng; > public function __construct(?RandomNumberGenerator $rng = null) > { > $this->rng = $rng ?? new XorShift128Plus(random_int(PHP_INT_MIN, > PHP_INT_MAX)); > } > public function nextInt(): int {} > public function getInt(int $min, int $max): int {} > public function getBytes(int $length): string {} > public function shuffleArray(array $array): array {} > public function shuffleString(string $string): string {} > public function __serialize(): array {} > public function __unserialize(array $data): void {} > } > > class XorShift128PlusNumberGenerator implements RandomNumberGenerator > { > public function generate(): int {} > public function __serialize(): array {} > public function __unserialize(array $data): void {} > } > > class MT19937NumberGenerator implements RandomNumberGenerator > { > public function generate(): int {} > public function __serialize(): array {} > public function __unserialize(array $data): void {} > } > > class SecureNumberGenerator implements RandomNumberGenerator > { > public function generate(): int {} > } > ``` > > This is an approach similar to Nikita's `createDefault`. If the > constructor has a null argument, it uses the default, XorShift128+, > internally. > > Also, whether the Random class is serializable or clonable depends on the > instance of RandomNumberGenerator being used. This means that when the > Random class clone is called, the `$rng` member will be implicitly cloned. > > How about this? > Hello, thanks for thinking again. A few editorial notes: - I guess that would be "new XorShift128PlusNumberGenerator" (like the class name)? - The non-Secure implementations' stubs are probably missing `public function __construct(?int $seed = null) {}`? - The Random class' and non-Secure implementations' stubs are probably missing `public function __clone(): void {}` (like `__serialize()`)? That would let us use the API like this: 1. Construction: one of: - default RNG and seed: `$random = new Random();` - chosen RNG, default seed: e.g. `$random = new Random(new MT19937NumberGenerator());` - chosen RNG and seed: e.g. `$random = new Random(new MT19937NumberGenerator(1234));` (no "default RNG, chosen seed", but the default RNG can be documented, and one could argue that a chosen seed only makes sense with a chosen RNG anyway) 2. Usage: `$int = $random->nextInt();`, `$percent = $random->getInt(0, 100);`, `$dword = $random->getBytes(4);`, `$shuffledList = $random->shuffleArray($list);` etc. I think this is well-consistent with the "pure design" described by Nikita, and I personally find it both flexible/extensible and easy-to-use =) (Just beware that the namespace question will probably pop up again.) PS: I feel like my numerous questions/suggestions (in this thread and the previous ones) may also have caused some deviations, so I hope that I won't need more and that other participants will reach a consensus... Best regards, -- Guilliam Xavier --0000000000009b44a405c4b685e3--