Page 1 of 1

Random Numbers

Posted: Thu Mar 27, 2003 7:28 pm
by bobthebobert
How do you do them in PHP?

random function in php

Posted: Thu Mar 27, 2003 7:51 pm
by phpfreak
hi there,

the following script generates 10 numbers randomly from 1-10


<?php
$count=0;


while ($count!=10)
{
$number=rand(1,10);
//rand is the random function and it chooses between number 1 and 10
//(inclusive of themseleves)
echo "the number now is: $number";
echo "<br>";
$count=$count+1;
}
?>

hope it helped!!!

Posted: Fri Mar 28, 2003 12:57 am
by volka
but keep in mind
http://www.php.net/manual/en/function.rand.php wrote: In older versions of PHP, you had to seed the random number generator before use with srand(). Since 4.2.0 this is no longer necessary.

Posted: Fri Mar 28, 2003 4:03 pm
by bobthebobert
If I just want one random number, I can just code:

$number=rand(1,10);

right?

And if so, does using this method give numbers with decimals too, or not?

Posted: Fri Mar 28, 2003 5:02 pm
by volka
int rand ( [int min, int max])
yes, you can do that, but no fraction.