Random String Goodyness
Posted: Wed Jan 09, 2013 10:33 pm
I am trying to build an API for the MovieStop website, and I'm using a random string generator to produce an API key, I notice that, when I tell it what length I want, it spits out a different length key. My code is as follows:
<?php function randomstring($length) /* This forms a string of a random lengeh*/ { $combined = array_merge(range("a", "z"), range(0,9)); shuffle($combined); $password = array_slice($combined, $length); $rand = implode("", $password); return $rand; } for ($i=1;$i<20;$i++) { $keygen = randomstring(0); echo "{$keygen} - ". strlen($keygen) ."<br>\n"; } ?>The "for" loop is to check and see if they're all a common length. Even though I specify a length of 0, I get a key length of 36. My output is as follows
osaem3fnpk6429divgrl1cuqxyhtb7z08jw5 - 36 s9ldkmtyrezuw618hcvpianq3x02o74gjb5f - 36 5agrj9obqp6lxt1w02c3vish8muyken47fzd - 36 dv4l9mo10sk2rwiat73nx5ubp6c8ghqefzjy - 36 asm1tq3neyoxjhgiub4068fzpk9v5wr27dlc - 36 pq0kzivwy3tgcs5ba1ul2dh68fr94ox7jenm - 36 embr2ys9j03dfxn751p46giqtkalo8uchvwz - 36 5ukdpe7ibc6q4mzws2x0rt9j3o1vlafgy8hn - 36 5guypoftwb42a6mlis3z7ke08r9qcxdhn1jv - 36 sc8x1dlmzujgok64neqi0p5yh2w9trbav37f - 36 dr4iwv0xs6zg3bn98plja25hecf1ut7qymko - 36 t0hxvr81pgwd345lemyqzj2fu6oc7nsb9aik - 36 4pshf2yto8mg97dxwive36ja1luqnrkzcb05 - 36 g93xl5zafyenbs718jwoptcim0k4vuh2r6qd - 36 upz8g9s5r4nkqbfdxljoec72a1hwvti360ym - 36 bh1nfcvatmxdrw27l4ekjsiz6g9p5q3o0u8y - 36 iojsmxvz0ackl2946uhdrbfepw18qyn5g7t3 - 36 7abc2fsn3z9ieko6xp08rwld14vugq5jtymh - 36 oq0dzuncsx9l23fjkta5w4e7brv86hgpy1mi - 36While not annoying, because I will just set the field length to 36, I would like to know what is wrong