1. Computing & Technology

Discuss in my forum

Random Numbers

How to Generate a Random Number with PHP

By , About.com Guide

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

©2012 About.com. All rights reserved.

A part of The New York Times Company.