Page 1 of 1

Questions Regarding "Randomizing"

Posted: Sun Oct 27, 2002 1:02 pm
by Tom
Hey all,
Since I'm still very new to PHP, I decided to learn how to randomize things. First I tried returning random numbers. Here's the script:

Code: Select all

<?php 
srand((double)microtime()*1000000);

$randomNo = rand(0,10); 

echo $randomNo;
?>
I don't believe there's any errors in the code, but no matter how many times i refresh, the only numbers returned are 7, 8, 9, and 10. Why don't the numbers 0-6 ever get returned?

My next question is, what exactly does the "(double)" -

Code: Select all

srand((double)microtime()*1000000);
in that line mean? Where can I learn about it?

Also, regarding the count() function - When it counts, does it start at 1 instead of 0?

Thanks for your help. - Tom

Posted: Sun Oct 27, 2002 1:22 pm
by hob_goblin
with the script:

Code: Select all

<?

srand((double)microtime()*1000000); 

$array = array();

	for($i = 0; $i < 10; $i++)&#123;
	$array&#1111;"$i"] = 0;
	&#125;

for($i = 0; $i < 250; $i++)&#123;
	$ran = rand(0,10); 
	$array&#1111;"$ran"]++;
&#125;

echo "<pre>";
print_r($array);
echo "</pre>";
?>
i got:

Code: Select all

Array
(
    &#1111;0] => 24
    &#1111;1] => 22
    &#1111;2] => 22
    &#1111;3] => 15
    &#1111;4] => 26
    &#1111;5] => 25
    &#1111;6] => 25
    &#1111;7] => 17
    &#1111;8] => 19
    &#1111;9] => 29
    &#1111;10] => 26
)
so maybe your system is messed up.

learning php

Posted: Sun Oct 27, 2002 3:36 pm
by phpScott
There are many great tutorials on this site as well as
http://www.php.net
Is a great resource for info and more tutorials. :D

phpScott

Posted: Mon Oct 28, 2002 1:26 am
by Takuma
This is the script I use:-

Code: Select all

&lt;?php
	srand((double)microtime()*1000000);
	$separater	=	md5(uniqid(rand()));
?&gt;

Posted: Mon Oct 28, 2002 2:00 am
by volka
if you do not trust php's random generator, on most (recent) unix systems there is a file called /dev/random . As many bytes as wanted can be read from this file - could take a while for large amounts.

just kidding ;)

Hmm..

Posted: Thu Oct 31, 2002 10:41 pm
by Tom
I still havn't had any luck with getting numbers 1-6 while trying to get a random number from 0-10 :cry:

Later guys,
Tom

Posted: Fri Nov 01, 2002 3:01 am
by volka
which version of php, OS?
may we see your script?