Sunday 16 March 2014

Random Number generation in php

randor generation in php
Random Number generation in php programming language. At some point of programming in php you are going to need of generate a random numbers. Let see how to create a random number then how to give lower and upper limit of generating random number. When we use rand function we can supply none arguments otherwise we can supply two arguments.

Code for random number generation (Screenshot1)

random number generation in php
Screenshot1



<?php



$rand = rand();

echo $rand();



?>

Output (Screenshot2, Screenshot3) :-


random number generation in php
Screenshot2

random number generation in php
Screenshot3
Every time you refresh we can see examples it will automatically generates random number with its lower limit which is 0 or 1 to upper limit which is predefined. Now rand function is already inbuilted function in php which we can use it. rand function generates random number which is unique so everytime you refresh it will automatically reset.

Code for random number generation (Screenshot4)

number generation
Screenshot4


<?php



$rand = rand();

$max = getrandmax();

echo $rand().'/'.$max;



?>

Output(Screenshot5):-

random number php
Screenshot5
For rand function maximum upper limit till which you can generate number without passing argument is 32767. As you can find getrandmax function to find maximum generate value of the rand function. As you can see in example max value of rand function remains same will refreshing browser and it only generate random numbers.

As we can pass argument to rand function for this i had great idea to create one samll php project play game rolling dice in php script. Its fun use of rand function.

There  are other reasons to use rand functions are like when we are uploading file we may want to specify a random array if you like of numbers , characters letters and thing like that. rand function is useful for creating more random value everytime you can also combine letters and characters of your choice   


No comments:

Post a Comment

Thanks For Comment Will get you Soon..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates