Random numbers in PHP

3 posts Page 1 of 1
Contributors
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Random numbers in PHP
MrAksel
This was originally ment to be in JavaScript, but I suggest looking at comathi's tutorial then :)
So there is one simple function in PHP to generate a random number. int rand(int $min, int $max). You do not have to specify the minimum and maximum value, they are optional, but useful if you need a number between a set range. The default min value is 0, and there is no default max, it varies from platform to platform. If you want to see the 'default' use the int getrandmax() function.
A sample PHP script could be:
Code: Select all
<?php
echo "Default max value:\n"
echo getrandmax()
echo "\nRandom number between 10 and 50:\n"
echo rand(10, 50)
?>
For greater performance use the int mt_rand(int $min, int $max) function. It generates a better random value with better performance. To get the default max value use int mt_getrandmax instead of int getrandmax().
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

Re: Random numbers in PHP
comathi
Nice :D I'm glad you found another tutorial to make ;)
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Re: Random numbers in PHP
MrAksel
Haha no problem :)
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
3 posts Page 1 of 1
Return to “Help & Support”