Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121335 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 35414 invoked from network); 16 Oct 2023 12:57:52 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 16 Oct 2023 12:57:52 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4D15E180511 for ; Mon, 16 Oct 2023 05:57:51 -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,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS24940 176.9.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from chrono.xqk7.com (chrono.xqk7.com [176.9.45.72]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 16 Oct 2023 05:57:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bastelstu.be; s=mail20171119; t=1697461068; bh=ezo5Gymxmu35tav1Pkzq0LWhJvKtygLi67IwIXbdDg8=; h=Message-ID:Date:MIME-Version:Subject:To:References:From: In-Reply-To:Content-Type:from:to:cc:subject:message-id; b=K4qdc3lU78sX3yYpn3d/453XGRNfCUk0DmL3zv5ZnmmeZ7nBTCLk4sHrwBiwtkp9X OcDSlbeR5nZRDzm7ultr8lwqSL4flZYNmJAJzmZmX2gOJQbdHnhT5B59p5IX8pEqnR FaUmBGfuRqBAj0nPV1L/1i5p+aDhLbB6LjASkvAz8w9OtaskpEy/U4Lsbd8imDtEtY pYRmmmJGhBofh8r+1WkJmoLNxfLpZ5/aAaNOZeaZGbOyYTjFb2L+kWFdu9fR595dCS VazlWci00FcaU1A7rdXaGlMvFV8iZrt2iZdSWmJP/BEzBGTjmwUHY0xP/q1B63fq1b r/hI8a8darTJg== Message-ID: Date: Mon, 16 Oct 2023 14:57:47 +0200 MIME-Version: 1.0 Content-Language: en-US To: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] Better name for method Randomizer::nextFloat() From: tim@bastelstu.be (=?UTF-8?Q?Tim_D=C3=BCsterhus?=) Hi On 10/16/23 10:34, Christian Schneider wrote: > Am 15.10.2023 um 19:24 schrieb Tim Düsterhus : >> Making getFloat(float $min = 0.0, float $max = 1.0, IntervalBoundary $boundary = IntervalBoundary::ClosedOpen) would seemingly make it legal to call ->getFloat(0.5), which I consider to be worse than nextFloat(). > > While I understand that you find getFloat(0.5) undesirable I would not consider it illegal, e.g. getFloat(max:5). > Additionally I would consider getInt(max:100) to be a very valid usage too. I don't, because I prefer explicit calls. If the upper bound is specified explicitly, the lower bound should be as well. In fact the `0,` is even shorter than spelling out `max:`. >> Side note: The implementation of nextFloat() is much more efficient than that of getFloat(0.0, 1.0) and the output will differ even for the same seeded engine. The domain of legal return values is identical. > > This sounds like an implementation detail as I'm pretty sure you could switch to that fast path easily enough if neither of the arguments were given. Yes, that's why it's a side note. > Side-note: The case of nextInt() is a bit trickier and strikes me as a bit of a weird API and I'd consider dropping it too: Having the max value depend on the Randomizer engine makes the generated values unstable in regards to switch the Engine. And if I read the documentation correctly then it only generates 32 bit values (or should it be 31 bits as it returns positive values only?). I think it would be clearer to require the Engine to provide at least 4 bytes and then specify the default max value of getInt() to be 2^31, optionally overridable with something up to PHP_INT_MAX. Yes, Randomizer::nextInt() is terrible. Let me provide the context why it exists like it exists: 1. The Randomizer was designed to be drop-in compatible with the existing randomness functionality (mt_rand, array_rand, shuffle, strshuffle) when combining it with the Mt19937 engine. 2. Thus mt_rand is equivalent to Randomizer::getInt(). 3. It is legal to call mt_rand() without parameters. 4. This behavior was carried over into getInt(). 5. During the final review, it was noticed that this is an overloaded function and a decision was made not to do this, because overloaded functions are terrible. 6. Thus the getInt()-without-parameters call was split into nextInt(). I think that the goal of making the new API a drop-in replacement has been a useful goal to make it easier to migrate and I assume that's why no one questioned whether retrieving "an arbitrary positive integer" is even useful in the first place. While writing the documentation I first realized how useless that is, because I was unable to write something useful for Randomizer::nextInt(), but at that point it was way too late. Hindsight is 20/20. Given that knowledge, is naming `nextFloat()` like `nextInt()` a mistake? I'm not sure. It was the obvious naming choice to me back when I wrote the RFC a year ago and no one pointed out how the name was not ideal, not during the RFC discussion phase, nor during the vote or in the half year leading up to the feature freeze. Now 6 weeks before the gold release, with 4 release candidates released and several blog posts written, the method name suddenly is a bad choice? > So in summary I'd support both adding default values to getInt()/getFloat() as well as dropping nextFloat() in favor of getFloat() (and possible nextInt() with getInt()). > I'd be on board with deprecating nextInt() in PHP 8.4. I'd also be on board with creating a better-named alias for nextFloat(), but I believe that it should remaing a standalone method, because of its usefulness. Best regards Tim Düsterhus