|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 10:00:01 2025 UTC |
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"; }