How Rdoanm is Rondam?
Being able to issue random strings or integers in programming has long been a topic of fascination. Although this article doesn't attempt to tackle the profound questions on randomisation because, well, I'm not at all good at maths, it does teach you how you can make the possibilities of getting the same random number so slim it's almost impossible.
The truth is that nothing is truly random, everything is based on something. For instance, I could throw out the word "Mash Potato" to you right now. Is it totally random? Or is it because I had it for my tea 5 nights ago? I could even pick out the 5 of diamonds from a stack of cards, seemingly totally random, but my option was based on something in my mind telling me to choose the card from that position.
The same goes for a computer. A computer can never be truly random. It can only throw out a random value based on the information it is fed. I could write the longest set of functions in the world in my PHP script, but it'd still boil down to certain factors. I've given it an IP address and it's given me 3 random numbers based on that IP address back to me, it's not random, it's just based on what the function does to return those 3 numbers to me.
All the philosophical stuff aside, however, we can return a fairly random string. Take the following example and ascertain whether or not it'd be viable in a small to medium sized application.
So what's the solution? In the interest of usability, you must find a nice spot between giving yourself a lot of possibilities with a very slim chance of repetition, and speed. Take the following as an example:
However, there is another function worth mentioning in PHP that could help us in our randomisation dilemma,
Note: The second option for mktime() is optional. If unspecified then mktime() will return the amount of seconds between Unix epoch and the current time and date.
The way the folks at YouTube do it with their random values for video links, is by storing the unique ID in the database and then checking to see if it is unique. If it isn't, generate a new 1 until it's unique. Remember though, nothing is truly unique, without having anything to check the random value against we can only make the possibility of repetition so narrow and unlikely it's highly impossible.
Good article. A small mention of srand and mt_srand (mt_round being the preferred method) may have been beneficial as they allow you to seed the random number generator; the latter using the Mersenne Twister algorithm.
Regarding seeding the random number generator:
Well in that case, my only observation would be to suggest
|All times are GMT. The time now is 01:46 PM.|
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0