Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114453 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 66018 invoked from network); 12 May 2021 16:27:12 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 12 May 2021 16:27:12 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A15851804D8 for ; Wed, 12 May 2021 09:35:15 -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_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-f42.google.com (mail-ej1-f42.google.com [209.85.218.42]) (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, 12 May 2021 09:35:14 -0700 (PDT) Received: by mail-ej1-f42.google.com with SMTP id u21so35877802ejo.13 for ; Wed, 12 May 2021 09:35:14 -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=F9eSUu5s8WRjyl6DFQ/g0cuG8yITTKd2aD0ey6Zvx78=; b=nXf8NETzi3XhiAVE330rXN9HuYRO3ozoTDcS+OwNwNXHHGQBmq5CKNGPJ39hQtp28M 0o3SO8ONvY/XtrOm8tx8iK+UoqnusBvEsXTO+1FotLlWWwonvncjpB8Gsih0yju7NU/c TXRtQmI2te5wJEK069pPsfhQQnYbxU9Th7z2vsugP1/Fu/GVMyDrSuPjB8oK0/AGjlsJ o+K5KMgxQnYnJRwHpItKSVPdIVwHjy9Fd9oJ9XL1ZVNpNOFCcw7neAa6npToVG+jyT3Z 0OwwoQtyu3VDxawB7xrCSo1ctbWg8Sx9DJnD/XlnWDxd5BLEihabhvOMbXapodAyMKAm gnAg== 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=F9eSUu5s8WRjyl6DFQ/g0cuG8yITTKd2aD0ey6Zvx78=; b=mOtOH/c1yx4zksBFhKN/vjm4SnbhE2kGmDMYOq+5ynqDl/FUs2/JYL3DQn1bUwpsyr QoyWflcItpHGw+Td/YglIx2jfFLC5NYnyNfqpMOwDFLQzQQAvdPS+XoUFo3yn5oUNszh HAP316+VP6TK9bRv4oKAmJuQVsyVd4wnxEEPHunlesYDPxU/aty+U3+p423aW2oEBYTG /djMcXzquwOasae8e4PSa06J8+X4V0phtx0l0mU8GIHNG4tGhlsJ+AmKfxUqPb2bV5hI We0Qt/ajFjgkLPh+noFFdNlXTjSC6TbwPVunxN31S+IqXiFi25aoqcQadJR7Go4ATSML KUFg== X-Gm-Message-State: AOAM531zEbc+k+hs4H1RgrLqxLDIFhhqnNbo+/JF+t8KDoRpQRtgrR5E Jtk6gYejOecwADivfTl7ARzPYccwekX+YRIPID9U09CD9UnCeg== X-Google-Smtp-Source: ABdhPJznhzmXQErvQFKoHDaMwXLdHirLI42iRfnAoHLiV9iRyeAOTMm/GEXs2qftvGWBhNbkouwBpCMmplDIrsbi/rs= X-Received: by 2002:a17:906:f909:: with SMTP id lc9mr37478283ejb.164.1620837311974; Wed, 12 May 2021 09:35:11 -0700 (PDT) MIME-Version: 1.0 References: <85544020-CC01-4AA0-941E-73ED95E496A6@cschneid.com> In-Reply-To: <85544020-CC01-4AA0-941E-73ED95E496A6@cschneid.com> Date: Wed, 12 May 2021 19:34:55 +0300 Message-ID: To: Christian Schneider Cc: php internals Content-Type: multipart/alternative; boundary="0000000000003811b505c224983c" Subject: Re: [PHP-DEV] Discussion: Object-scoped RNG From: drealecs@gmail.com (=?UTF-8?Q?Alexandru_P=C4=83tr=C4=83nescu?=) --0000000000003811b505c224983c Content-Type: text/plain; charset="UTF-8" On Wed, May 12, 2021 at 6:43 PM Christian Schneider wrote: > Am 11.05.2021 um 23:20 schrieb Larry Garfield : > > 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 --0000000000003811b505c224983c--