Tuesday, 10 December 2013

Random Number generation in php

Random Number 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 generatingrandom number. When we use rand function we can supply nonearguments otherwise we can supply two arguments.
Code for random number generation (Screenshot1):-
Random Number Generation
Random Number Generation (Screenshot1)
1
2
3
4
5
6
<?php
 
$rand = rand();
echo $rand();
 
?>
Output (Screenshot2, Screenshot3) :-
Random Number Generation
Random Number Generation (Screenshot2)
Random Number Generation
Random Number Generation (Screenshot3)
Every time you refresh we can see examples it will automaticallygenerates random number with its lower limit which is 0 or 1 to upper limit which is predefined. Now rand function is already inbuilt-ed function in php which we can use it. rand functiongenerates random number which is unique so every-time yourefresh it will automatically reset.
Code for random number generation (Screenshot4):-
Random Number Generation
Random Number Generation (Screenshot4)
1
2
3
4
5
6
7
<?php
 
$rand = rand();
$max = getrandmax();
echo $rand().'/'.$max;
 
?>
Output(Screenshot5):-
Random Number Generation
Random Number Generation (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 randfunction. As you can see in example max value of rand functionremains 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