View Full Version : creating a string of random numbers and letters
I know how to use rand() to create a random number but how can i create a string that is 7 charactors that is a mixture of numbers and letters. Thanks.
Jason
04-10-2003, 11:23 PM
I don't know if there is a function that can do this. You could possibly use two random number generators, one to chose between an array of letters or numbers and the second to either choose which letter (position) in the array or a random number. That is the only way that comes to me right now...off the top of my head....
Jason
Jason
04-10-2003, 11:55 PM
not that I know of....do a random number from 1-2 that will chose whether you are putting in your 7element array or whatever a number or letter....then the next one would be say for letter: a number that is from 1-26 for each letter of the alphabet and then for number: would be a random number....and loop 7 times....does that make sense?
Jason
okay this is what i made..
<?php
$v = 1;
while($v <= 7)
{
$randnumberagain = rand(1,4);
if($randnumberagain%2)
{
$randome_alph = rand(1,26);
if($randome_alph == 1){$add = "a";}
if($randome_alph == 2){$add = "b";}
if($randome_alph == 3){$add = "c";}
if($randome_alph == 4){$add = "d";}
if($randome_alph == 5){$add = "e";}
if($randome_alph == 6){$add = "f";}
if($randome_alph == 7){$add = "g";}
if($randome_alph == 8){$add = "h";}
if($randome_alph == 9){$add = "i";}
if($randome_alph == 10){$add = "j";}
if($randome_alph == 11){$add = "k";}
if($randome_alph == 12){$add = "l";}
if($randome_alph == 13){$add = "m";}
if($randome_alph == 14){$add = "n";}
if($randome_alph == 15){$add = "o";}
if($randome_alph == 16){$add = "p";}
if($randome_alph == 17){$add = "q";}
if($randome_alph == 18){$add = "r";}
if($randome_alph == 19){$add = "s";}
if($randome_alph == 20){$add = "t";}
if($randome_alph == 21){$add = "u";}
if($randome_alph == 22){$add = "v";}
if($randome_alph == 23){$add = "w";}
if($randome_alph == 24){$add = "x";}
if($randome_alph == 25){$add = "y";}
if($randome_alph == 26){$add = "z";}
}
else
{
$add = rand(1,9);
}
$randstr = $randstr;
$randstr .= $add;
$v++;
}
$randstr = strtoupper($randstr);
echo $randstr;
?>
Jason
04-11-2003, 12:24 AM
$randstr[7];
for ($i =0; $i < 7; $i++){
$j = rand(1,2);
if ($j ==1){ //alphabet
$alpha = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y' ,'z'}
$k = ran(0,25);
$randstr[$i]=$alpha;
}
else { //numbers
$randstr[$i] = $ran(0,9);
}
}
thats an ugly way...didn't test this but you get the idea....
Jason
firepages
04-11-2003, 04:36 AM
uniqid() creates a random string... its mostly numeric but you can manipulate that somewhat , the last version here produces an alpha-numeric mixed case unique string pretty good for password generation etc, grabbing the last 7 characters gives better values for whatever reason (I assume because uniqid works on the current timestamp?)
<?
echo substr(uniqid(""),-7).'<br />';
echo substr(md5(uniqid("")),-7).'<br />';
echo substr(base64_encode(md5(uniqid(""))),-7);
?>
missing-score
04-11-2003, 08:11 AM
I think:
$str = '';
for($i = 0; $i < 7; $i++)
{
$str .= chr(rand(ord("a"),ord("Z")));
}
Hope this helps... :thumbsup:
$str = substr(md5(rand(0,99999)),0,7);
seems fastest. might need to salt the rand if your build is oldish though.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.