Search Perform an advanced search query SOFTPEDIA
 
SOFTPEDIA
Updated one minute ago
HomeSubmit a program for being reviewedAdvertise on our websiteGet help on surfing our websitesSend us your feedbackGet information about our XML/RSS backend and how to use itBrowse the news archiveVisit our discussion forumVizitati forumul in limba romana



KLIP
  1. HOME
  2. SCIENCE
  3. TECHNOLOGY
  4. WEBMASTER
  5. SECURITY
  6. MICROSOFT
  7. LINUX
  8. APPLE
  9. GAMES
  10. TELECOMS
  11. REVIEWS
  12. LIFE & STYLE
  13. EDITORIALS
  14. INTERVIEWS
  15. RSS
Welcome!
Hello, Guest

Login if you have a Softpedia.com account.

Otherwise, register for one.

TIPS AND TRICKS

Random Numbers in PHP

- Basic Functions to Generate Random Numbers

By: Catalin Bocanu, Web News Editor

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
<?php
echo mt_rand(1,50);
echo rand(1,50);
?>


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.




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
 
Comments | Link here | Subscribe
Print | Send to friend
Today's News | Yesterday's News

Search:


17th January 2008, 21:55 GMT | Copyright (c) 2008 Softpedia | Contact:
Read by 2,015 user(s) | Rating: | 6 vote(s) so far | Cast your vote:
Random Numbers in PHP - USER OPINIONS




We are sorry, there are no opinions available for this article.






SHARE YOUR OPINION ABOUT Random Numbers in PHP

Since you are not logged on, your comments will have to be approved before being displayed.
Click here to login, or register.
Your Name:
Your Email:
Type in the result:
Your Opinion:
 


DO YOU WANT TO CONTACT US?  

If you have some comments or you want to send us some information you can send us an email directly to .
You can use the form below for the same purpose.
Your full name: (at least 3 characters)
Your email address: (at least 5 characters)
Message subject: (at least 5 characters)
Message text:
(at least 10 characters)
Type in the result:
 
 



© 2001 - 2008 Softpedia. All rights reserved.
Softpedia™ and Softpedia™ logo are registered trademarks of SoftNews NET SRL.
Copyright Information | Privacy Policy | Terms of Use | Contact Softpedia | Update your software | Archive