måndag 27 april 2009

Creating a random name generator with PHP






While working on my auto-generated entry-level NPC's for my onlinegame, I noticed I wasn't really happy with the names that was generated. And since I spent time to make it sex-independent (males and females get seperate names), I decided to write a short article of how to create a random name generator using PHP. This guide is fantasy/rpg-based, but the principle can be applied to real names or what-have-you.

There are many ways to do it, this is simply one of them. First off you'll need to decide if you want the names to be compiled by two or more parts, or if you simply want to get a random name from a list of names. I will show how to create names compiled by three parts, which will be the prefix, infix, and suffix (beginning, middle, and end). The scripts can however be modified to use more or less parts to create the random names.

The reason behind me using three parts is because 1) it allows for a broader spectrum of possible names, and 2) gives me the option to use more combinations of masculine and feminine derivatives, both in the prefix and the traditional suffix. If you're interested in doing a good name-generator, I suggest you read up on derivatives.

You can either decide to use arrays which contains the names, use a database, or textfiles. I find the use of a database overkill, and the use of arrays can get messy with a bad overview if you have many parts and many combinations. But for a small range of combinations and alternatives, arrays might be an easier option, and the code-example can easily be modified to use arrays instead of including the files in the first step.

Now, on to the code. We'll show the feminine version.


# Add the parts to arrays
$prefix = file("includes/1_female.txt");
$infix = file("includes/2.txt");
$suffix = file("includes/3_female.txt");

# Randomize them
shuffle($prefix);
shuffle($infix);
shuffle($suffix);

# Pick a (randomized) value
$prefix = rtrim($prefix[0]);
$infix = rtrim($infix[0]);
$suffix = rtrim($suffix[0]);

$name = "" .$prefix ."". $infix ."". $suffix ."";
#Also works, but are considered "ugly":
# $name = "$prefix$infix$suffix";



*First part adds the contents of the files to variables as arrays, which means first row has the 0-key, second row has the 1-key, etc.
*Next we'll need to randomize the content. There are several ways of doing this, I personally prefer shuffle(), since it makes things look more tidy. You could also count the lines of the file to a variable to use as the key with rand(0,sizeof($var)-1), and then use $prefix[$var] at the third step to get a random line from the file, though I personally think it looks ugly. Nevertheless, it's an option.
*Third part is to pick out a random entry. Since we used shuffle(), we can use any valid key to get a random value, since we won't know the order of the array unless we peek inside it. First entry is always a good option, so we'll use the first key (0).
*Last part is to simply put the pieces together. I decided to give an extra option of how to do this, since both do the same thing, and the massive usage of quotationmarks can be abit disctracting for new programmers.

A very simple example of how the text-files are composed (suffix overlapping the prefix) using 2 parts to compose the names. Might be worth mentioning that when using infixes and suffixes, clever usage of blank lines works, and adds an extra dimension to the names, making some names to be shorter. But if you'd use the type of list below I'll guarantee that your names will look butt-ugly.


The result can be viewed on top. It's hardly meeting any fantasy-standards, and some names might be abit of a tongue twister, but should at least serve as an example of what the end result would look like. Both sexes uses the same infix, but I use different prefixes and suffixes for the masculine and feminine names. See the first part of the code-example and you can probably figure out what needs editing to add masculine derivatives.

Inga kommentarer:

Skicka en kommentar