• Register

For all web developpers and webpage layout makers, also for beginners who desire to learn! :-)

Post tutorial Report RSS How to create a random name generator?

A step-by-step guide how to create a simple name generator.

Posted by on - Basic Server Side Coding

A simple name generator can be used in e.g. web RPG games to create random names (in the registration form).

First, the exact result must be worked out in detail. What would we like to achieve? 4-letter names? 5-letter names? How long should they be? Also, names consist of both consonants and vowels. Such distinctions are very important in thinking up the exact script. We can divide the name into vowels and consonants.

The general pattern can be:
CVCVV
C - consonant
V - vowel
So we can get results like Beloa, Delou, Geloe, etc. (two consonants, three vowels).
And yes, we'd need to have names with the first letter in the upper case.

Now we know what we're looking for. Let's get to the heart of the work: the code itself. We've divided the name in two groups: consonants and vowels. We can group them by using various methods, e.g. as variables ($a = "a", $b = "b", $c = "c"). Instead, we'll use a bit simpler and compact way: ARRAYS.

We'll use simple arrays, w/o declaring any keys, because we'll need the basic ones (0, 1, 2, etc.).

<?php

$vowels = array("a", "e", "o", "u");
$consonants = array("b", "c", "d", "v", "g", "t");

?>

Two arrays are declared. Notice that not all vowels and consonants were included in these groups. We don't need the whole alphabet - just these letters what we'd like to have. You can expand on them as you wish - also, you can take example of e.g. the Roman alphabet, or Roman names (look which consonants/vowels are required) to achieve particular name patterns.

What's next?
We have things that will be chosen - now we need a hand to choose them. We'll need some functions that randomly select one letter out of the whole array.

Fortunately, PHP authors included a very useful function called array_rand() that select the KEY, *emphasis*, KEY (not the value, i.e. not "a", "e", "b", "c", but 1, 0, 2, etc.) at random.

The pattern (parameters) of array_rand():
array_rand(array, number_of_chosen_keys)
//e.g.
//array_rand($arrayVariable, 1);

function randVowel()
{
	global $vowels;
	return $vowels[array_rand($vowels, 1)];
}

First, why "global $vowels"? The instruction "global" make the PHP parser know that you use a global variable, not the variables inside the function. You can't normally access e.g. other variables, i.e. variables outside functions - functions are like separate files, or completely different worlds.
And the "global" instruction, this key word is the portal between these two worlds - the "general" one and the world of this very function.

That's why you need to put "global" before $vowels.
You can also move array (cut & paste) inside function, so it'd look like this:

function randVowel()
{
	$vowels = array("a", "e", "o", "u");
	return $vowels[array_rand($vowels, 1)];
}

Now, for little explanation of return instruction and the whole function choosing one of the vowels.

"Return" keyword returns the specific value as a result of the entire function. If your function was

function sumUpThese()
{
	$sum = 5 + 5;
	$sum2 = 10 + 10;
	return $sum;
}

$result = sumUpThese();
echo $result;

The $result variable would hold number 10. Why 10, not 20? Because the function was made to return $sum variable, not $sum2.
The entire function is then equal to 10, and when you assign it to $result, $result gets only 10. All other processes (variables, nested functions, etc.) inside functions are lost after it is parsed.

Now we need to take a look at the entire line again:
return $vowels[array_rand($vowels, 1)];

Let's split it up in two parts:
$vowels[];
and
array_rand($vowels, 1)

You know that we use the built-in function array_rand to obtain a random numeric key pointing to a letter. Parameter "1" is meant to choose only ONE letter, not two. More than 1 will result in getting a part of an array, in an array form - we need to avoid that for the purpose of getting only the number (not an array).

$vowels[];
or rather
$vowels[0];
is fetching the FIRST (0) value of the array $vowels, "a".
$vowels[1];
would fetch "e"
$vowels[2]
would get "o"

So, array_rand() function is selecting a random key number (0, 1, 2, or 3, because we have 4 vowels: a, e, o, u), and then it is put inside $vowels[] array variable, which transforms the number into a vowel.

The whole function returns the VOWEL then, not the number.

Everything's ready, except a few things. Please, make a function for consonants yourself, treat it as a challenge - you can include these consonants: b, c, d, v, g, t (the exact order isn't important).

The last thing you need to do, when you are done with the function for the random selection of a consonant, is the actual display of randomly generated name.

echo "" . randConsonant() . "" . randVowel() . "" . randConsonant() . "" . randVowel() . "" . randVowel() . "";

"" .
or
. "" .
. ""
are used to separate these functions w/o creating any spaces.

Notice that I don't assign the function to a variable, as in:
$v = randVowel();
Why? Wouldn't it be easier?

It would, but when you assign the function to variable, you call the function only ONCE. And that means you'll get results like "kokoo", "dedee", "vuvuu". You need to call the function each time to have different results.

If you have the entire code, you'd notice that the generated name is completely in the lower case.

To cap the first letter, you need to use ucfirst() function.

echo ucfirst("" . randConsonant() . "" . randVowel() . "" . randConsonant() . "" . randVowel() . "" . randVowel() . "");

And now for the whole code, including the consonant function:

<?php

$vowels = array("a", "e", "o", "u");
$consonants = array("b", "c", "d", "v", "g", "t");

function randVowel()
{
	global $vowels;
	return $vowels[array_rand($vowels, 1)];
}

function randConsonant()
{
	global $consonants;
	return $consonants[array_rand($consonants, 1)];
}

echo ucfirst("" . randConsonant() . "" . randVowel() . "" . "" . randConsonant() . "" . randVowel() . "" . randVowel() . "");

?>

Well, it's an example how to make a primitive name generator. Most probably you'll use AJAX or JavaScript for that purpose, because you should avoid any actions/script that would interrupt filling up the registration form. PHP itself needs validation or redirection to another page, i.e. in this example you can make a button "Generate a random name!" that will display the generated name - but in another page (if you don't want it to be displayed when visiting the page, i.e. you don't want it to be shown at the very start).

Therefore, in the registration form something like that would be impossible, since it'd interrupt the registration itself.

That's why JavaScript or AJAX, or anything that can create content dynamically at the same page, would be feasible.

Remember, it's only an example how to make a simple generator. You can make different types of names, e.g. Roman, Gothic, etc (giving the player an option to choose the name type for better effect). Depends on the game you're creating. Also robotic, futuristic names can be made using the same design/scheme. You just need to work out the exact patterns / functions creating the name.

Post comment Comments
jjawinte
jjawinte - - 5,067 comments

Your a pretty sharp guy Feillyne.

Reply Good karma Bad karma+2 votes
Guest
Guest - - 689,975 comments

Very helpful and well-explained :) Stumbled across this thanks to a random google search

Reply Good karma Bad karma+1 vote
Guest
Guest - - 689,975 comments

This comment is currently awaiting admin approval, join now to view.

Guest
Guest - - 689,975 comments

This comment is currently awaiting admin approval, join now to view.

Guest
Guest - - 689,975 comments

This comment is currently awaiting admin approval, join now to view.

Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: