Softpedia
 

NEWS CATEGORIES:



NEWS ARCHIVE >>
SOFTPEDIA REVIEWS >>
MEET THE EDITORS >>
Home > News > Webmaster > Tips and Tricks

January 17th, 2008, 21:55 GMT · By Catalin Bocanu

Random Numbers in PHP

SHARE:

Adjust text size:


PHP Logo
Enlarge picture
Generally, numbers are widely used in day-to-day life, as well as in computer applications. Every programming language has built in specific functions to deal
with numbers.

In PHP programming language, the numbers are classified in two main categories: integers and floating point numbers. The advantage of this programming language reside in its capability to automatically convert integers to floating point numbers and reverse. For example, the value of a fraction written in the form 5/2 will be interpreted as being 2.5.

The true problem in numbers manipulation occurs when you need to generate random numbers for a certain purpose. The properties of random numbers will not be discussed in this article, but they could be of different types, depending on their destination of usage. In server side scripting, random numbers generators and functions that produce them are employed in creation of the security systems as captcha images, password encryption applications, games and more.

In PHP programming language there are two basic functions that returns random numbers: rand(a,b) and mt_rand(a,b). The a and b parameters represents the minimum, respectively the maximum integer value defining the range in which the random number will be produced. If those functions are called without parameters, they will return a random integer situated between 0 and a maximum value, which depends on the operating system type.

The mt_rand() function uses Mersenne Twister algorithm for random number generation, which means that the quality of random numbers is much better as compared to those resulted from the call of rand() function, because the last one is based on a simpler random number generator that creates predictable random numbers sequences. For example, if you run the next code many times, you will notice the difference in random numbers generation between mt_rand() and rand() functions:

CODE

In practice, it is recommended to select and test a certain random numbers generator using statistical methods in order to determine the random numbers "quality". This aspect is very important in PHP applications based on random numbers, like a random image display script or an ads rotating application.

TELL US WHAT YOU THINK:

8,775 hits · Link to this article · Print article · Send to friend · Subscribe to news

MUST-READ RELATED ARTICLES:


Get The Optimal Free PHP Web Hosting Service

A Simple Polynomial Equation Solver Script

A Promising Alternative for Google Reader

A Universal PHP Script to Interact with Any MySQL Database

The Simplest Contact Form Script with HTML and PHP

READER COMMENTS:



No user comments yet.
Be the first to express your opinion!
Copyright © 2001-2012 Softpedia. Contact/Tip us at

WindowsGamesDriversMacLinuxScriptsMobileHandheldNews

SUBMIT PROGRAM   |   ADVERTISE   |   GET HELP   |   SEND US FEEDBACK   |   RSS FEEDS   |   UPDATE YOUR SOFTWARE   |   ROMANIAN FORUM