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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
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

Pull Requests

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-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 02 14:01:36 2025 UTC