How to Use Random Value Function in PHP?

Friends, today we will understand through this article about the random function of PHP, so for this we have a function rand() inside it we pass 2 parameters min and max inside it we put any 2 value when Also we refresh the page.So any random value in between will pick up and give.

Another function similar to this comes, we call it mt_rand(), the work of both is same but it is 4 times faster than rand. In this also two parameters are passed. The full form of min and max mt is Mersenne twister algorithm.

And there comes another function similar to this lcg_value() does not give us any value above 0 and 1.

<?php
echo (rand() ."<br>");
echo (rand() ."<br>");
?>
Output:-
1276548932
1078650210

If we do not give any parameter, now this value will keep changing as many times as you keep referring to the page. As you can see in the above code.

<?php
echo (rand(0 ,10 ) ."<br>");
echo (rand(0 ,100 ) ."<br>");
?>
Output:-
8
18

Now I have given a limit by passing the parameter, the value should come within it. Now let’s see using the new function as well.

<?php
echo (mt_rand(0 ,10 ) ."<br>");
echo (mt_rand(0 ,100 ) ."<br>");
?>
Output:-
10
0

Even if the parameter is not given, it will still work like the rand function.

<?php
echo (lcg_value() ."<br>");
echo (lcg_value() ."<br>");
?>
out put :-
0.765438987653217
0.065785432987600

Note: Here you can see lcg_value does not pass any parameter inside it and it returns us a float value between 0 and 1 like you can see in the above code.