1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Random Numbers

How to Generate a Random Number with PHP


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

Related Video
Basic PHP Syntax
Using PHP With HTML
  1. About.com
  2. Technology
  3. PHP / MySQL
  4. Learn PHP
  5. Generate a Random Number With PHP

©2014 About.com. All rights reserved.