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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.