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)
Screenshot1 |
<?php $rand = rand(); echo $rand(); ?>
Output (Screenshot2, Screenshot3) :-
Screenshot2 |
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)
Screenshot4 |
<?php $rand = rand(); $max = getrandmax(); echo $rand().'/'.$max; ?>
Output(Screenshot5):-
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..