Random String Goodyness

Ask about a PHP problem here.
Post Reply
wrichards8
Posts: 66
Joined: Thu Jan 12, 2012 3:54 pm
Contact:

Random String Goodyness

Post by wrichards8 »

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:[syntax=php]<?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";
}
?>[/syntax]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 [syntax=text]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 - 36[/syntax]
While not annoying, because I will just set the field length to 36, I would like to know what is wrong
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Random String Goodyness

Post by ExtremeGaming »

I believe it won't work with specifying 0 because php will assume you mean the max (I think). a-z and 0-9 always adds up to 36 which supports that theory.
<?php while(!$succeed = try()); ?>
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Random String Goodyness

Post by Helx »

Why would you want a length of 0 anyway?
wrichards8
Posts: 66
Joined: Thu Jan 12, 2012 3:54 pm
Contact:

Re: Random String Goodyness

Post by wrichards8 »

I don't, I just wanted to see what it would output, The point of going for 0 was to demonstrate my point (that you could put in any number and it'd return a string of a different length). I got a string length of 26 when I passed '10' to the function, 13 gave 23. I'm guessing there is just something I've missed here
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Random String Goodyness

Post by jacek »

Interesting problem :P You are actually getting max_length - length as the result. The reason being that array_slice takes three parameters array_slice($array, $start, $length) and you missed off the middle one which made $length behave like $start. to fix this just specify 0 as $start

[syntax=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, 0, $length);
$rand = implode("", $password);
return $rand;
}[/syntax]
Image
Post Reply