php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #2887 mt_rand(int min, int max) doesn't seem to use min and max
Submitted: 1999-12-01 15:39 UTC Modified: 2000-05-22 14:14 UTC
From: mikeuser at hotmail dot com Assigned:
Status: Closed Package: Misbehaving function
PHP Version: 3.0.11 OS: WinNT
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mikeuser at hotmail dot com
New email:
PHP Version: OS:

 

 [1999-12-01 15:39 UTC] mikeuser at hotmail dot com
BUG! (WinNT)<BR>

Setting min-max range doesn't work! I get values well beyond range.
<PRE>
echo "<B>mt_rand(0,20)=".mt_rand(0,20)."</B><BR>";


Actual results:
mt_rand(0,20)=879286
mt_rand(0,20)=1357383
mt_rand(0,20)=286308
etc...
</PRE>

A work-around using the % operator:

<PRE>
$randnum = $mt_rand() % ($maxVal-$minVal) + minVal;
</PRE>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-05-22 14:14 UTC] hholzgra at cvs dot php dot net
mt_rand() divided by RAND_MAX instead of MT_RAND_MAX
on some platforms, espc. win32, theese are not
equal, and so the results are to low/high

fixed in CVS, PHP4 version was OK
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 17 03:01:32 2024 UTC