Bug #72784 [Asn]: BC break in rand(): throws warning in 7.1 and returns FALSE for max-min range

From: Date: Mon, 08 Aug 2016 20:43:18 +0000
Subject: Bug #72784 [Asn]: BC break in rand(): throws warning in 7.1 and returns FALSE for max-min range
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-203097@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72784&edit=1

 ID:                 72784
 Updated by:         cmb@php.net
 Reported by:        bugs dot php dot net at majkl578 dot cz
 Summary:            BC break in rand(): throws warning in 7.1 and
                     returns FALSE for max-min range
 Status:             Assigned
 Type:               Bug
 Package:            Math related
 PHP Version:        7.1.0beta2
 Assigned To:        leigh
 Block user comment: N
 Private report:     N

 New Comment:

> I think the easiest course of action is to relax the strict
> min/max ordering on mt_rand().

I wouldn't want this lax behavior in the long run, though. Maybe
allowing it for now with a deprecation notice would be okay.
Probably some discussion on internals is appropriate.


Previous Comments:
------------------------------------------------------------------------
[2016-08-08 16:08:21] leigh@php.net

This will be a side effect of rand() now being an alias of mt_rand(). So unfortunately both
accidental and intentional.

I think the easiest course of action is to relax the strict min/max ordering on mt_rand().

------------------------------------------------------------------------
[2016-08-08 15:34:49] cmb@php.net

Confirmed: <https://3v4l.org/l7i5e>.

There have been several fixes to rand() et. al. which have been
proprosed as on voted upon in
<https://wiki.php.net/rfc/rng_fixes>.

I'm not sure whether this change was desired, or just by accident.
Leigh?

------------------------------------------------------------------------
[2016-08-08 15:13:57] bugs dot php dot net at majkl578 dot cz

Description:
------------
When rand() is given parameters in the reverse order - max first, min second, it now throws warning
and returns FALSE. In all previous PHP versions it did not return any warning and returned the a
number from the range.

This is especially tricky in scripts where rand() is supplied arguments from variable, it'd be
now required to write rand(min($a, $b), max($a, $b)) instead of just rand($a, $b).

Although I understand the reasoning behind requiring specific order, returning FALSE is a BC break
and should not be introduced in 7.x (warning is IMHO fine though).

Test script:
---------------
var_dump(rand(5, 1));

Expected result:
----------------
int(N)

Actual result:
--------------
Warning: rand(): max(1) is smaller than min(5) ...
bool(false)


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=72784&edit=1


Thread (7 messages)

« previous php.bugs (#203097) next »