0

I'm randomly generating chars in PHP, using:

for ($i = 0; $i < $length; $i++) {  
    $generatedCode .= chr(rand(97, 122));  
}

But I dont want the letters "g" and "m" to be in the randomly generated string, how can I do this?

user229044
  • 232,980
  • 40
  • 330
  • 338
beans
  • 1,765
  • 5
  • 25
  • 31

5 Answers5

9
$chars = array_diff(range('a', 'z'), array('g', 'm'));

$generatedCode = '';
for ($i = 0; $i < $length; $i++) {  
    $generatedCode .= $chars[array_rand($chars)];
}
deceze
  • 510,633
  • 85
  • 743
  • 889
  • 2
    OP, doing loops until you hit the right char is wrong. This is what you need. – Leonid Shevtsov Oct 27 '11 at 14:27
  • I would clean it up slightly with `while (strlen($generatedCode) < $length) { ... }` instead of introducing a counting variable, but this is still the best solution. – user229044 Oct 27 '11 at 14:36
  • @meagar Computationally that's slightly more expensive though, so... meh. :) – deceze Oct 27 '11 at 14:39
  • @deceze You assume, but I doubt it. I've benchmarked this assumption in the past, and it's no longer true, if it ever was *in PHP*. It is not a language for micro-optimization. – user229044 Oct 27 '11 at 14:40
  • 1
    @meagar It's not much, but a little... http://codepad.org/kG59sxEM :) It certainly isn't worth discussing about, whichever style floats your boat is fine. – deceze Oct 27 '11 at 14:44
2
for ($i = 0; $i < $length; $i++) {
    do {
        $randChar = chr(rand(97, 122));
    } while($randChar != 'g' && $randChar != 'm');
    $generatedCode .= $randChar;
}
user229044
  • 232,980
  • 40
  • 330
  • 338
Emir Akaydın
  • 5,708
  • 1
  • 29
  • 57
1

Do the loop until the correct values are in place.

for ($i = 0; $i < $length;) {  
    $code = chr(rand(97, 122));  
    if ($code !== 'm' && $code !== 'g') // add the correct values here
    {
         $i++
         $generatedCode .= $code;
    }
}
user229044
  • 232,980
  • 40
  • 330
  • 338
Rene Pot
  • 24,681
  • 7
  • 68
  • 92
1

Put simply, filter out 'g' and 'm';

<?php
$length = 8;
$generatedCode = '';
while( strlen( $generatedCode ) < $length ) {  
    $random = chr(rand(97, 122));
    if( !in_array( $random, array( 'g', 'm' ) ) ) {
        $generatedCode .= $random;  
    }
}

echo $generatedCode;
Berry Langerak
  • 18,561
  • 4
  • 45
  • 58
0
for ($i = 0; $i < $length; $i++) {  
    $generated = chr(rand(97, 122));
    if ($generated != "g" && $generated != "m") {
       $generatedCode .= $generated;  
    }
    else{
       $i--;
    }
}
genesis
  • 50,477
  • 20
  • 96
  • 125
  • 1
    The `else $i--` is kind of ugly. Why not only increment in the correct case, instead of incrementing every time and decrementing in the incorrect case? – user229044 Oct 27 '11 at 14:27
  • +1 @meagar - why not `while (in_array($generated = chr(rand(97, 122)),array('g','m'))) continue; $generatedCode .= $generated;` – DaveRandom Oct 27 '11 at 14:59
  • @DaveRandom Because that is even uglier. You could make it slightly less ugly by negating your condition and having an empty loop body, instead of a while loop containing a single `continue;` statement, which is evil. Poor abused while loop :( – user229044 Oct 27 '11 at 15:02