As Always PHP MAIN!
^^Go to the PHP main for all of your web dev needs! And maybe some chocholate milk.
Ok today you will be learning about random functions. Shall we begin?
The Different Random Functions are used in a variety of different ways. Many of them are used for id's but you can use them for anything imaginable. First off lets learn them.
Code ::
<?php
rand();
?>
The Code I just showed above is used to simply create a random number. It will not output it or anything. Great right? Yeah, but only one problem. Lets say for some un-realistic reason I need it to ONLY create a random number beteween 1 and 100. It seems hard right? Not really, just put your smallest number , and your largest number possible into the () brackets.
So, I want a number beteween 1 and 100. Well My Code would look like this!
Code ::
<?php
rand(1,100);
?>
The rand() function can create any random number from 1 to 32768. That number (32768) is not 100% accurate because the limit depends on the operating system you work on. But Im not going to get to deep into that.
So our limit for the rand() function is 32768...EH! Well suppose that you need to get a random number from a larger range. Well the rand() function wont be able to do that so you will needs to use the....
mt_rand() Function! Swoooooooch!
This function enables us to generate a random number from 1 to Any number you can imagen. Except for 1 fufillion (as seen on austin powers) (but that doesnt exist) anyways as said earlier, that function gives us the power of a larger range of random numbers.
Nooooooow! We want to output a random number, like get the magical php to echo"" our code. Welly welly well its not that hard!
For the random function echoing its not too hard.
Code ::
<?PHP
echo(rand(1,2000))
?>
That simple code will output a random number based beteween 1 and 2000! Amazing!
And to echo "" a mt_rand() number you would use this simple code.
Code ::
<?PHP
echo(mt_rand(1,1000000))
?>
That my friends would echo a number beteween 1 and the stupidly large number I gave after. Now I hope that you have all learnt from my tutorial. Im thinking of writing a tutorial explaining how the random number is generated. And I hope you all enjoyed my tut. =) thanks,
and!
GO TO THE PHP MAIN!
PHP MAIN:: For all your PHP dev needs!