php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46097 rand maxes out after a few calls.
Submitted: 2008-09-16 16:39 UTC Modified: 2008-11-08 01:00 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: RQuadling at GMail dot com Assigned:
Status: No Feedback Package: Math related
PHP Version: 5.3.0alpha2 OS: Windows XP SP3
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: RQuadling at GMail dot com
New email:
PHP Version: OS:

 

 [2008-09-16 16:39 UTC] RQuadling at GMail dot com
Description:
------------
Hi.

I was testing an encryption/decryption routine by passing it random lengths of random data.

I realized that in a very short amount of time, all the strings being tested were the same length.

I've reduced the string building code to the bare minimum.

Reproduce code:
---------------
<?php
$i_Count = 0; // By the 6'th loop, we are stuck on 32767.
while(++$i_Count <= 20)	{
	$s_Data = '';
	$j = rand();
	for($i = 0 ; $i < $j ; ++$i) {
		$s_Data .= chr(rand(0, 255));
	}
	echo $i_Count, ' ', $j, ' ', strlen($s_Data), "\n";
}



Expected result:
----------------
1 randomvalue1 randomvalue1
2 randomvalue2 randomvalue2
3 randomvalue3 randomvalue3
4 randomvalue4 randomvalue4
5 randomvalue5 randomvalue5
6 randomvalue6 randomvalue6
7 randomvalue7 randomvalue7
8 randomvalue8 randomvalue8
9 randomvalue9 randomvalue9
10 randomvalue10 randomvalue10
11 randomvalue11 randomvalue11
12 randomvalue12 randomvalue12
13 randomvalue13 randomvalue13
14 randomvalue14 randomvalue14
15 randomvalue15 randomvalue15
16 randomvalue16 randomvalue16
17 randomvalue17 randomvalue17
18 randomvalue18 randomvalue18
19 randomvalue19 randomvalue19
20 randomvalue20 randomvalue20

Actual result:
--------------
1 randomvalue1 randomvalue1
2 randomvalue2 randomvalue2
3 randomvalue3 randomvalue3
4 randomvalue4 randomvalue4
5 randomvalue5 randomvalue5
6 32767 32767
7 32767 32767
8 32767 32767
9 32767 32767
10 32767 32767
11 32767 32767
12 32767 32767
13 32767 32767
14 32767 32767
15 32767 32767
16 32767 32767
17 32767 32767
18 32767 32767
19 32767 32767
20 32767 32767

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-10-26 19:08 UTC] jani@php.net
What if you actually seed it first? See http://php.net/srand
 [2008-10-31 10:39 UTC] RQuadling at GMail dot com
Other than "Note: As of PHP 4.2.0, there is no need to seed the 
random number generator with srand() or mt_srand() as this is now 
done automatically."

No difference if the srand() is OUTSIDE of the loop.

Inside the loop, then I get my random data correctly.



<?php
$i_Count = 0; // By the 6'th loop, we are stuck on 32767.
while(++$i_Count <= 20) {
	srand();
	$s_Data = '';
	$j = rand();
	for($i = 0 ; $i < $j ; ++$i) {
		$s_Data .= chr(rand(0, 255));
	}
	echo $i_Count, ' ', $j, ' ', strlen($s_Data), "\n";
}
 [2008-10-31 15:52 UTC] jani@php.net
Have you tried with the non-ZTS build?
 [2008-11-08 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Aug 03 21:01:24 2020 UTC