A group about Web Design & Development. Ask and Learn everything related to web design and development.
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:
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.).
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.
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():
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:
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
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:
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).
is fetching the FIRST (0) value of the array $vowels, "a".
would fetch "e"
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.
. "" .
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.
And now for the whole code, including the consonant function:
Therefore, in the registration form something like that would be impossible, since it'd interrupt the registration itself.
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.