Welcome All
PHP MAIN :
Welcome Everyone To My Tutorial. Today you will be learning about the mt_rand() , and the array_rand(); functions. Well shall we begin? The mt_rand() function is the brother of the rand() function. mt_rand() allows users to generate a random number. Just using the simple function will generate a random number without any limits. But lets suppose that you want to put a limit. Suppose that I want to give a thread an ID from a range of 1 to 100. Well you would have to put that into the brackets ().
<?PHP
mt_rand(1,100);
?>
See how we put the 1 and 100 into the brackets. The reason we do that is because the first number in the brackets is interpreted as the lower number of the range. While the second number is interperated as the higher number of the range. So in short, you can create random numbers using the mt_rand() function and set limitations on it.
Now you must be thinking what can I use mt_rand() for? Well there are tons of possibilies. First off you could use the mt_rand() function for a members system, to assign the user for a random ID, and the best part about it is that you can give it limitations. So if you want your mysql database to assign the user a radom number which is 2 digets. You could use:
$userid = mt_rand(10,100);
or you could use the mt_rand() function to assing topics a spisific ID. The possibilites are endless. Thats it for mt_rand();
Now I will begin to teach you about array_rand();. First off, the array_rand() function is used to randomly select a variable from an array.
Heres an example:
<?php
$a=array("a"=>"Dfox","b"=>"Pilot-Doofy","c"=>
"Elbekko");
print_r(array_rand($a,1));
?>
Thats a simple example of using a array_rand() function. What can these be used for. As said earlier, many possibilities, maybe to create a validation code, or to make a random id, which includes letters. The possibilites again are endless.
What is there in a comparision to the array_rand() function and the mt_rand() function? Well In my personal opinion after reading about both, the array_rand() function is used for creating random intergers with limits, while the mt_rand() function is used to create random keys, such as letters + numbers persay. Which one would I personally use for creating a login script or any php script. My answer would be both, because they are both VERY useful functions which you can use in any situation. I hope you enjoyed my tutorial, and that I hit it on the head this time. Thanks,
- PHPGeek
Hope I did better than last time =)