00:00
00:00
Newgrounds Background Image Theme

Kingkavvvv just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

PHP random functions

1,531 Views | 8 Replies
New Topic Respond to this Topic

PHP random functions 2007-01-28 15:30:58


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!

Response to PHP random functions 2007-01-28 15:33:13


Hope you all enjoyed it! Give me feedback please. Its my first tut.

Response to PHP random functions 2007-01-28 15:40:57


You kinda missed the point of where they can be useful, why mt_rand() is better other than supporting larger numbers and the array_rand() function.

Oh well, it's rather nice apart from that ;)


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

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature

Response to PHP random functions 2007-01-28 15:42:25


At 1/28/07 03:40 PM, elbekko wrote: You kinda missed the point of where they can be useful, why mt_rand() is better other than supporting larger numbers and the array_rand() function.

Oh well, it's rather nice apart from that ;)

Thanks bekko =) Ill explain better for my next tutorial =)

Response to PHP random functions 2007-01-28 16:00:11


Um, it's kinda useless considering that most of what you said there is documented in the php manual. Definitely should have covered array_rand() and provided some examples.. all very well saying it can be used for IDs, but then not actually following that through and explaining it is a bit pointless really.

http://uk2.php.net/rand


> twitter.

Response to PHP random functions 2007-01-28 18:24:16


Yea, I was hoping you would explain what's it useful for. Creating validation codes, etc. And as someone else stated, I would suggest using mt_rand() instead of it's older brother rand(). You could also go in depth about MySQL random numbers and stuff.

Maybe explain how random numbers work? Not sure, but it wasn't very fulfilling to me.


Merkd.com - It Pays to Play

Earn real money by betting and investing; or, sponsor, challenge, compete,

recruit, communicate, network, earn money playing games, and much more.

Response to PHP random functions 2007-01-28 20:03:18


OK, it's a good first attempt, but allow me to make some suggestions.

First of all, you got a little too much in to telling how a function works. The aim of this tutorial was to teach about the actual random functions PHP allows you to choose from, not explain what a function is in the programming sense.

Really get into the functions. Keep it organized, make sure you cover most of the functions, and EXPLAIN exactly what they do. What I mean is don't worry about echoing them, but worry about explaining their usage, show real life examples, and stuff like that.

I think this is a really important lesson (php random functions) so I really hope you can make some changes to this and re-submit it.

Good try, I just think you need a tiny bit of work, but you're definitely getting there :)

Response to PHP random functions 2007-01-28 22:24:03


Thanks guys, Ill re write it, and explain how random numbers are generated next time. Thanks for the feedback.

Response to PHP random functions 2007-01-28 22:32:10


At 1/28/07 10:24 PM, PHPgeek wrote: Thanks guys, Ill re write it, and explain how random numbers are generated next time. Thanks for the feedback.

Awesome. One more quick suggestion.

I love how you tried to make it funny and entertaining, but a LITTLE less on the stuffing and focus more on the meat.