1. Home
  2. Computing & Technology
  3. PHP / MySQL

Random Numbers
How to Generate a Random Number with PHP

By Angela Bradley, About.com

There are lots of different reason you may want to generate a random number. PHP can generate a random number over a very large range, or pick randomly between only two or three choices. You can set these parameters when you generate the number.

Random numbers are created using the rand () function. The rand function has two parameters. They are the lowest number and the highest number possible of being generated. If you wanted to create a number between 1 and 100 you would use rand(1, 100);. If you do not specify a range, it will create a number between 0 and getrandmax(). On some platforms the default max is limited to 32768 and if you want a higher cap number you need to specify the range in the function.

Here are some simple fun scripts that practice using random numbers:

Show Random Banners

Flip a Coin

Guess the Number Game

Random Quotes

More PHP / MySQL Quick Tips
Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Learn PHP
  5. Generate a Random Number with PHP - PHP Random Number

©2009 About.com, a part of The New York Times Company.

All rights reserved.