Jump to content

Random number letter combo


joe92

Recommended Posts

I am trying to make a function that will generate a random number letter combo but the letters can only be abcde, no others. It must come out in the format, 1a~3d~9b~ etc. This is what I have so far:

 

function randomNumLet($amount){
$theNos = array('1', '2', '3', '4', '5', '6', '7', '8', '9');
$theLet = array('a', 'b', 'c', 'd', 'e');
$combo = '';
for($i=0;$i<$amount;++$i)
	{
		$rand_num = array_rand($theNos, 1);
		$rand_let = array_rand($theLet, 1);
		$combo .= $theNos[$rand_num[0]].$theLet[$rand_let[0]].'~';
	}
return $combo;
}


$num_let = randomNumLet(2);
echo $num_let;

 

And it returns just '~~' with no numbers or letters. I'm a bit stuck here and would appreciate any help. How can I make the function output 1a~4d~ etc?

 

Cheers,

Joe

Link to comment
https://forums.phpfreaks.com/topic/250517-random-number-letter-combo/
Share on other sites

Ahh, I was going about this the complete wrong way! Got it working now:

 

function randomNumLet($amount){
$theNos = array('1', '2', '3', '4', '5', '6', '7', '8', '9');
$theLet = array('a', 'b', 'c', 'd', 'e');
$combo = '';
for($i=0;$i<$amount;++$i)
	{
		$num = count($theNos);
		$rand_num = rand(0, ($num - 1));
		$let = count($theLet);
		$rand_let = rand(0, ($let - 1));
		$combo.=$theNos[$rand_num].$theLet[$rand_let].'~';
	}
return $combo;
}


$num_let = randomNumLet(2);
echo $num_let;

 

Its now counting the array and generating a random number between 0 and the size of the array - 1. Sorted,

Joe

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.