Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119192 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 39182 invoked from network); 20 Dec 2022 09:25:02 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 20 Dec 2022 09:25:02 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 0C946180504 for ; Tue, 20 Dec 2022 01:25:01 -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,HTML_MESSAGE,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-qt1-f171.google.com (mail-qt1-f171.google.com [209.85.160.171]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 20 Dec 2022 01:25:00 -0800 (PST) Received: by mail-qt1-f171.google.com with SMTP id c7so10459581qtw.8 for ; Tue, 20 Dec 2022 01:25:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=colopl.co.jp; s=google; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=/6aaBB6S/TuDGSHdigFntjMZsrhUOUKfVtHRYfyYO+U=; b=Ia35kq1EGeEBiCI2qV7WOEJhbA5dy+Ww9MLWdvQwDs7iM9lZcLUaSexSjt8TcW7C1N 1AXkFE9NbfBpci01RAssHZm8FAmoem/HSLsD+gfAnrhdjYha+uOl8e+E9rFgteE4R5sR 1ThxvDHjyWQrVtTArfTWHivCE2l6GZr+wEEMYxhZrot+edXPNU62RV+WWKjLHeq9CY9c 13DIyK1FHcCpKAgJCSpWan+c1Mac12Cecd+2jlFXXa/7ctw92jTsx77na+6New/gO5xM 1Cl2Y+hvNbrmgq5zdzb51mgWxh1/JtfrSv1AaZ2wKGHHANIZWp31S3L0nbdIYWCkz0sS x3oQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=/6aaBB6S/TuDGSHdigFntjMZsrhUOUKfVtHRYfyYO+U=; b=Ahw6AGBXjRfSUy8omxRwUuFT9eDdPlgxWGcLQiufm5LkROII40AyhA+v3yX7s6DjHm mceDmOg5apEOHA8NgGjFyitekIwfvJED8hcx6wu8BTEjWhAe6lo8AzEoCP/jPkbndlJ8 j1RS3DZw56KUTweeT/wtolzG0bY/U8NlPFgo2vkW7PAmmiP+W5uJLtSqr/nzKp6SRpXr Wqt7I18EMEHNE/azQgGliXdnVCg5Zip9ZIzKcug0UvbbOfFQONE5rsQ3z62d3cFK52dA vCSZVKh5VB8Ox1et8Am9dJw2z1f6pjkafHathneT1cNEyKCd016AtSWXjc3i3sui10EH 61tw== X-Gm-Message-State: ANoB5pk9PgUxFBlJ3N/o0URnVeVRDQdhmu8R0QSEyW3gHxyjoNwoF5/S IiPq5Plf1GQsNkuT47rPMEikhXdKMKi9My0xl7tCAnkYYP+nxTg= X-Google-Smtp-Source: AA0mqf7oXyCLy1Ctpz5IO99+/XC9+qEHvjoV6NF8EO1d9ryL3DFUr4JvWVpkxdg38tfDcoVPHRC8O8goJO/p0vW0kVI= X-Received: by 2002:ac8:71c7:0:b0:3a6:7f4e:764c with SMTP id i7-20020ac871c7000000b003a67f4e764cmr38350834qtp.114.1671528299460; Tue, 20 Dec 2022 01:24:59 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 20 Dec 2022 18:24:48 +0900 Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary="00000000000085932905f03f0240" Subject: Re: [PHP-DEV] ext-random: add random_float() ? From: g-kudo@colopl.co.jp (Go Kudo) --00000000000085932905f03f0240 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 2022=E5=B9=B412=E6=9C=8820=E6=97=A5(=E7=81=AB) 18:14 Go Kudo : > 2022=E5=B9=B412=E6=9C=8820=E6=97=A5(=E7=81=AB) 17:15 Hans Henrik Bergan <= divinity76@gmail.com>: > >> btw while we're on the topic, does anyone know if this function gives >> biased results/is-safe or not? i honestly don't know: >> function random_float(float $min, float $max): float >> { >> if ($min > $max) throw new \InvalidArgumentException("min must be >> less than max"); >> if ($min =3D=3D=3D $max) return $min; >> return $min + random_int(0, PHP_INT_MAX) / PHP_INT_MAX * ($max - >> $min); >> } >> >> >> On Tue, 20 Dec 2022 at 09:06, Hans Henrik Bergan >> wrote: >> > >> > >returns a value between 0.0 and 1.0. >> > >> > wouldn't it be better to follow random_int(int $min, int $max) design? >> eg >> > random_float(float $min, float $max): float >> > >> > On Tue, 20 Dec 2022 at 07:27, Go Kudo wrote: >> > > >> > > Hi Internals. >> > > >> > > Congratulations on the release of PHP 8.2. >> > > I just recently upgraded production PHP from 7.4 to 8.1 :) >> > > >> > > Now that my work is done, I was thinking about a proposal for a >> sunsetting >> > > of existing functions for PHP 8.3 based on the features introduced i= n >> > > ext-random, and I realized that there is no alternative to >> `lcg_value()`. >> > > >> > > Essentially, this could be completely replaced by >> > > Random\Randomizer::getFloat(), but there are easier `random_int()` a= nd >> > > `random_bytes()` functions for ints and strings. >> > > >> > > The Randomizer may be overkill and complicated for PHP use cases whe= re >> > > random number reproducibility is not required in many cases. >> > > >> > > So, why not add a `random_float(): float` function? This function, >> like the >> > > others, uses CSPRNG and returns a value between 0.0 and 1.0. This >> behavior >> > > is `Closed` `Closed`. >> > > >> > > Opinions are welcome. >> > > >> > > Regards, >> > > Go Kudo >> > > Hi. > > The dangers of generating floating-point random numbers from PHP integer > values are explained in detail in Tim and Joshua's RFC. > > https://wiki.php.net/rfc/randomizer_additions > > I missed it at first too, and was negative about adding > `Randomizer::getFloat()` ... > In general, I think this problem is less noticeable and the absence of th= e > random_float() function increases the likelihood of incorrect > implementations in userland. > > Regards, > Go Kudo > In the PHP-8.3 current development branch, you can already polyfill as follows However, it is complicated. ```php getFloat($min, $max, $intervalBoundary); } ``` --00000000000085932905f03f0240--