Newgrounds.com — Everything, By Everyone.

Checking login status…

USERNAME:

PASSWORD:

Logging in…

Logged in as:
.
Logging out…
Inbox My Account Log Out


Forum Topic: PHP: mt_rand() and array_rand()

(461 views • 6 replies)

This topic is 1 page long.

<< < > >>
None

Mister-Mind

Reply To Post Reply & Quote

Posted at: 1/29/07 05:27 PM

Mister-Mind EVIL LEVEL 07

Sign-Up: 07/01/06

Posts: 2,213

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 =)

None

elbekko

Reply To Post Reply & Quote

Posted at: 1/29/07 05:44 PM

elbekko EVIL LEVEL 16

Sign-Up: 07/23/04

Posts: 6,306

It's, well, a bit better.
It's still lacking tho =/ Especially the use of the array_rand function.

"My software never has bugs. It just develops random features. " - Unknown

[ PHP: Main | Omigod, a blog ]

BBS Signature

None

Mister-Mind

Reply To Post Reply & Quote

Posted at: 1/29/07 05:45 PM

Mister-Mind EVIL LEVEL 07

Sign-Up: 07/01/06

Posts: 2,213

Adding On To My Last Post, Because I like History,

The Hisory of the mt_rand() function

Most of you probably already know but for those who dont, the mt_rand() function is the relative of the rand() function. The reason we use mt_rand() rather than the rand() function is because the rand() function can only generate a random number from one to 32768. Therefore depriving us of countless random intergers, unless a cap was put on it. But even so, we use the mt_rand() because it can get a random number from a larger range, (unless a cap is set)

I hope that was enough to fill your php thirst. =)


None

Zendra

Reply To Post Reply & Quote

Posted at: 1/30/07 03:19 AM

Zendra NEUTRAL LEVEL 37

Sign-Up: 09/07/03

Posts: 11,979

Basic, basic. But good in a way.

NG Review & BBS Moderator // PM Review & BBS Abuse


None

different

Reply To Post Reply & Quote

Posted at: 1/30/07 05:23 AM

different DARK LEVEL 34

Sign-Up: 07/08/04

Posts: 3,753

At 1/29/07 05:27 PM, PHPgeek wrote: 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();

Actually that isn't the way it's done. Most forum systems will use an auto-increment id on their forums/topics/posts/users - for example this post is id 11,663,530 - and having a random id for this type of thing would either mean id conflicts or you updating the code every month or so to cater for more topics.

simple & minimal, that's my style.
thoughts on ux, usability & design


None

Mister-Mind

Reply To Post Reply & Quote

Posted at: 1/30/07 06:58 AM

Mister-Mind EVIL LEVEL 07

Sign-Up: 07/01/06

Posts: 2,213

True, very true, auto-increment id would be a better id. I remember reading about that in SpamBurgers tutorial. Thanks for the comments. =)


None

authorblues

Reply To Post Reply & Quote

Posted at: 1/30/07 09:00 AM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,265

At 1/29/07 05:45 PM, PHPgeek wrote: The Hisory of the mt_rand() function

hardly the definative history. its based on the mersenne twister function, which is pseudo-random with a period of 2^19937. ya know, mersenne twister... mt_rand()...

my blog. smash code: 3523-1700-9216 [AUTHR]

BBS Signature

All times are Eastern Daylight Time (GMT -4) | Current Time: 04:44 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!