A group about Web Design & Development. Ask and Learn everything related to web design and development.

Report article RSS Feed How to create a random name generator?

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

Posted by feillyne on May 25th, 2010
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.).

code:
<?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);

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

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

code:
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.

code:
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.

php code:
echo ucfirst("" . randConsonant() . "" . randVowel() . "" . randConsonant() . "" . randVowel() . "" . randVowel() . "");

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

code:
<?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 May 26 2010, 1:10pm says:

Your a pretty sharp guy Feillyne.

+2 votes     reply to comment
Guest
Guest May 13 2013, 3:38pm says:

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

+1 vote     reply to comment
Post a Comment
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

Established
Nov 13, 2007
Privacy
Public
Subscription
Open to all members
Contact
Send Message
Email
Members Only
Membership
Join this group
Group Watch
Track this group
Tutorial
Browse
Tutorials
Report Abuse
Report article
Related Groups
Inside the Page
Inside the Page Hobbies & Interests group with 54 members
Web devs & web game devs
Web devs & web game devs Hobbies & Interests group with 35 members