Questions Regarding "Randomizing"

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Tom
Forum Newbie
Posts: 20
Joined: Sat Oct 05, 2002 5:24 pm
Location: Southwest FL

Questions Regarding "Randomizing"

Post 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
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

learning php

Post 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
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

This is the script I use:-

Code: Select all

&lt;?php
	srand((double)microtime()*1000000);
	$separater	=	md5(uniqid(rand()));
?&gt;
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ;)
Tom
Forum Newbie
Posts: 20
Joined: Sat Oct 05, 2002 5:24 pm
Location: Southwest FL

Hmm..

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

which version of php, OS?
may we see your script?
Post Reply