Is there any php function in our time, which generates absolutely unique, random string? Or rephrase the question, how to make absolutely unique and random string in php in our time? Is it enough to combine couple of randomizing functions?
4
-
possible duplicate of True random number generationMichael Petrotta– Michael Petrotta2012-06-25 05:25:58 +00:00Commented Jun 25, 2012 at 5:25
-
1This is not so much related to PHP as it is to the question whether any software program can generate a truly random string. It cannot (as all random generators are based on some algorithm, and therefore are deterministic). All software implementations of random generators are called pseudo random (number) generators (PRNG) because of this. Have a look at the link @MichaelPetrotta shared for more info.Joris– Joris2012-06-25 05:29:28 +00:00Commented Jun 25, 2012 at 5:29
-
Joris, there are cryptographically-secure PRNGs, though. You can safely use those.Joey– Joey2012-06-25 06:04:00 +00:00Commented Jun 25, 2012 at 6:04
-
BTW, the question is ill-posed, since it is not specified if the string should have a fixed length, or a maximum and/or a minimum length. Also, whether all bytes and byte sequences are allowed.Walter Tross– Walter Tross2012-06-25 06:05:36 +00:00Commented Jun 25, 2012 at 6:05
Add a comment
|
1 Answer
Just to be nitpicky... If it has to be unique then it's not really "random". Randomness includes the possibility of duplication, since generating a random state doesn't depend on (or refer to) previous states.
Do you just want to create a GUID? PHP has a couple methods for generating random IDs:
Some other people have also found ways to do this, it seems.
4 Comments
Joey
Regarding your last link: I'd be wary trusting a resource that calls MD5 encryption ... Option 4 is downright horrible, following neither Version 4 nor version 5 GUIDs. In fact, it's just an MD5 hash made to look like a GUID.
David
@Joey: Definitely a good point. It was found on Google, and seems like a decent starting point for further research.
verisimilitude
@All: How good is this as a random string generator for passwords ?
$str_password = str_shuffle('abcefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890@#%$*'); return substr($str_password, 0 ,$int_password_length);verisimilitude
Going through all the principles/answers/posts above, I think I'll need to rethink on my random string generator's implementation that does'nt seem random to me any more :)