1

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 generation Commented Jun 25, 2012 at 5:25
  • 1
    This 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. Commented Jun 25, 2012 at 5:29
  • Joris, there are cryptographically-secure PRNGs, though. You can safely use those. Commented 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. Commented Jun 25, 2012 at 6:05

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
@Joey: Definitely a good point. It was found on Google, and seems like a decent starting point for further research.
@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);
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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.