2
$list_ar = array();
for($x = 0;$x < 500; $x++){
    $val = generateRandomString(20);
    if(!in_array($val,$list_ar)){
        echo $x.'=='.$val.'<br>';
        array_push($list_ar,$val);
    } else {
        echo $x.'== IN ARRAY<br>';
    }
}

function generateRandomString($length){
    $characterlist = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789';
    $characterlist_array = str_split($characterlist);
    $id = '';
    for($a = 0;$a<$length;$a++){
        shuffle($characterlist_array);
        $position = array_rand($characterlist_array, 1);
        $id .= $characterlist_array[$position];
    }
    return $id; 
}

When I run the code above it gives in array false up to 360 lines after that it returns in array true. I'm expecting it to return in array false up to 500 lines I've been running the code more than 20 times and it gives the same result of exactly 360 lines in array false. Any ideas?

4
  • 4
    this code is HIGHLY ineffcient... why are you shuffling the entire charlist array, when you could just be generating a random INDEX key to lookup a char in that array? it's far easier generating a SINGLE ranumber number, e.g. $id .= $characterlist[rand(0,count($characterlist)-1)] than is to completely shuffle the array and then generate a random number to generate the key. Commented Dec 12, 2013 at 18:45
  • I don't get in array any times: codepad.org/7kRUTLRy Commented Dec 12, 2013 at 18:46
  • changed my code and based it on @MarcB suggestion and it worked. thanks! Commented Dec 12, 2013 at 19:21
  • There's a comment in the docs for array_rand here php.net/manual/en/function.array-rand.php#105265 That indicates that it has very strange randomness behavior (like, it might not actually be particularly random at all). I tried a couple of different ideas, and as long as I don't call that function, I can make this work. Commented Dec 12, 2013 at 19:27

2 Answers 2

1

Based on what Marc said what happens if you change your function to:

function generateRandomString($length){
    $characterlist = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789';
    $characterlist_count = strlen($characterlist)-1;

    $id = '';
    for($a = 0;$a<$length;$a++){
        $id .= $characterlist[rand(0,$characterlist_count)];
    }
    return $id; 
}

Does it still fail?

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

1 Comment

note that count(string) - 1 always returns 0. You still need the str_split
0

Since we are answering with alternatives:

function generateRandomString($length) {
    $characterlist = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789';
    $characterlist_array = str_split($characterlist);
    shuffle($characterlist_array);

    return implode(array_slice($characterlist_array, 0, $length));
}

Comments

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.