php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #1166 rand() max value is always 1 greater than it should be.
Submitted: 1999-02-19 22:18 UTC Modified: 1999-02-27 09:31 UTC
From: mike24 at bellsouth dot net Assigned:
Status: Closed Package: Misbehaving function
PHP Version: 3.0.6 OS: Redhat 5.1 Linux 2.0.35
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: mike24 at bellsouth dot net
New email:
PHP Version: OS:

 

 [1999-02-19 22:18 UTC] mike24 at bellsouth dot net
rand() always uses a max value 1 greater than inputed.

Consider the following code snippet:

<?php 
    srand((double)microtime()*1000000);
    $number = rand(5, 15);
    $echo "$number\n";
?>

$number should contain an integer between 5 and 15. However, this bug causes $number to contain an integer between 5 and 16.    

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [1999-02-27 09:31 UTC] sas
This was fixed by Rasmus (I believe)

revision 1.35
date: 1999/02/15 06:04:11;  author: rasmus;  state: Exp;  lines: +18 -10
A slight tweak
----------------------------
revision 1.34
date: 1999/02/15 04:04:48;  author: rasmus;  state: Exp;  lines: +23 -6
Fix the broken rand() and mt_rand() ranges

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 05:01:33 2025 UTC