Question:
php create random string?
anonymous
2011-05-04 18:19:30 UTC
hello everyone,

I have a mysql table with around 800 users so what I did is for each user I created a random password using a random string generator function I've created, it works almost to perfection but sometimes the random password gets repeated twice or even three time and I have no idea why its acting this way.

Could you guys give me your guesses on why this is happening,

Thank you in advance,

Mark
Three answers:
Robin T
2011-05-04 20:49:19 UTC
Randomness doesn't guarantee uniqueness. It is possible, however, that your random password generation is not random enough, making the probability to have duplicates high. Can't really tell without looking at your code though.
Ratchetr
2011-05-04 18:46:45 UTC
2 things come to mind:



Frist:

Duplicates are always possible in a randomly generated number. Ask 10 people to guess a number between 1 and 10. Sometimes 2 people in a row will guess 7. If your random number generator doesn't duplicate that behavior, it's broken ;-) The probability of duplicates decreases as you increase the length of the password, and/or the number of valid password characters. How long is your password, and what are the valid characters?



Second:

Did you generate these passwords all at once, in a script, or did you generate them over a long period of time?

If you used a script, did it reseed the random number generator each time through a loop using srand()? Did you seed based on the current time? You never want to do that...seed once...not every time through a loop. (time doesn't increment fast enough on fast computers to make that work).



Either way...bottom line is that 2 randomly generated passwords aren't guaranteed to be unique. You really should code for that possibility, no matter how slim the odds are:

do

generate password

if password not already used

    add password

while password already used;
kluesner
2016-12-04 03:56:55 UTC
right that's one i take advantage of. that's based from time so no 2 document names could be the same. that's an out placed of 32 characters yet you are able to shorten it. $The_File = "yourfilename.gif"; function my_file_name($TheFile){ $new_file_name = md5($The_File . time()); } and to shorten it $new_file_name = substr(md5($The_File . time()),0,15); or to maintain some letter of the unique document call and so on. $new_file_name = substr($The_File,0.5) . substr(md5($The_File . time()),0,15); the considerable section is the time(); that's the same element as date("U");


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...