Re: Random number generator

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Stephen wrote:
> On Thu, 11/6/08, WEISD <pzig@xxxxxxxxx> wrote:
>
>   
>>> That is strange. I get pretty balanced results on this
>>>       
>> computer.
>>     
>>> <?php
>>>
>>> $histogram = array_fill(1, 10, 0);
>>>
>>> $iterations = 200000;
>>>
>>> for ($i = 0; $i < $iterations; ++$i) {
>>>    ++$histogram[round(rand(1, 10))];
>>> }
>>>
>>> print_r($histogram);
>>>
>>> ?>
>>>
>>> Andrew
>>>       
>> Simple code,
>>
>> <?php
>> $number = rand(1, 10);
>>
>> include("footer$number.html"); ?>
>>
>> You can see it in action here at the bottom of the page
>> there is a footer. 
>> Each footer is the same right now except I have numbered
>> them for testing.
>>
>> As I refresh the page,  I get footer10 almost always with
>> an occasional 2 or 
>> 4 here and there...
>>
>> http://www.weisd.com/store2/WINHD-9022.php
>>     
> Computer functions to generate random numbers are not designed to do what their name suggests.
>
> Software testing requires repeatability, and this includes random number generation.
>
> Without knowing how PHP seeds the generator it is difficult to predict what it will do.
>
> I still think taking the last digit of the current time is your best solution.
>
> Stephen
>
>   
The PHP developers understood the random problem and the need for
predictability, so they did 2 things. 
1.  Randomly seed the random number generator every time
2.  Allow you to set the seed for predictability

http://us.php.net/srand

Thank you,
Micah Gersten
onShore Networks
Internal Developer
http://www.onshore.com



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux