Random Numbers

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
bobthebobert
Forum Commoner
Posts: 25
Joined: Sat Feb 15, 2003 5:56 pm

Random Numbers

Post by bobthebobert »

How do you do them in PHP?
phpfreak
Forum Commoner
Posts: 30
Joined: Fri Mar 21, 2003 10:28 am
Location: New Jersey,USA
Contact:

random function in php

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

Post 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.
bobthebobert
Forum Commoner
Posts: 25
Joined: Sat Feb 15, 2003 5:56 pm

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

Post by volka »

int rand ( [int min, int max])
yes, you can do that, but no fraction.
Post Reply