php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40187 mt_rand(), rand()
Submitted: 2007-01-21 19:06 UTC Modified: 2007-01-21 19:18 UTC
From: callawey at gmail dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.2.0 OS: windows xp
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: callawey at gmail dot com
New email:
PHP Version: OS:

 

 [2007-01-21 19:06 UTC] callawey at gmail dot com
Description:
------------
rand and mt_rand generates random lenght integer between -9999999999 and +9999999999. 

example mt_rand(100000000000, 999999999999);
outputs: -564, -688852, 5586666988, -68889558

Reproduce code:
---------------
$str = mt_rand(100000000000, 999999999999);

Expected result:
----------------
positive numbers between 100000000000 and 999999999999

Actual result:
--------------
-492911349,1162038978,-219333642

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-01-21 19:10 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Integer overflow on 32 bit systems is the cause.
 [2007-01-21 19:12 UTC] callawey at gmail dot com
while($row = $DB->fetchArray($result))
		{
			for($i=0;$i<=12;$i++)
			{
				$str .= rand(0,9);
			}
			//$str = mt_rand(100000000000, 999999999999);  
			
			$sqlc = sprintf("UPDATE %s SET user_id='%s' WHERE id='%s'", $DB->prefix("users"), $str, $row["id"]);
			$DB->queryF($sqlc);
		}



this code makes all colums -1
 [2007-01-21 19:18 UTC] callawey at gmail dot com
while($row = $DB->fetchArray($result))
		{
			$str = "";
			for($i=0;$i<=12;$i++)
			{
				$str .= rand(0,9);
			}
			
			$sqlc = sprintf("UPDATE %s SET user_id='%s' WHERE id='%s'", $DB->prefix("users"), $str, $row["id"]);
			$DB->queryF($sqlc);
		}

this returns same number everytime even i use $str = ""; in every loop Why ?
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri May 09 17:01:28 2025 UTC