NEWS CATEGORIES:



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

Tips and Tricks


Random Numbers in PHP

Basic Functions to Generate Random Numbers

By Catalin Bocanu, Web News Editor

17th of January 2008, 21:55 GMT

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.

TAGS:

Random Numbers | Random Numbers Generator | PHP Functions | PHP Programming | PHP Tips
Read by 5,117 user(s) | Add comment | Link to this article TWEET THIS


Article rating:
Very Good (4.0/5) 7 vote(s)    

Subscribe to news | Print article | Send to friend

© Copyright 2001-2009 Softpedia
Contact:

 

 

SEARCH THE NEWS ARCHIVE :




Today's News
| Yesterday's News | News Archive


MORE 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

A Simple Calculator with HTML Forms and PHP

Management of A Free Custom Search Engine

A Free Multilingual Real Estate Website

Free Hosting Services for WordPress Blogs

Web Based Proxies

User opinions:

No user comments yet.
Be the first to express your opinion using the form below!

Share your opinion:

Your Name:
Your Email Address:
(will not be used for commercial purposes)
Solve this to prove you're not a bot: =
Your review/opinion:

 




Windows tabGames tabDrivers tabMac tabLinux tabScripts tabMobile tabHandheld tabGadgets tabNews tab

SUBMIT PROGRAM   |   ADVERTISE   |   GET HELP   |   SEND US FEEDBACK   |   RSS FEEDS   |   ENTER NEWS SITE   |   ENGLISH BOARD   |   ROMANIAN FORUM