Just Writing About Creating A Random Name Generator Using PHP…
As a fiction writer I’m always dreaming up names for my characters. Sometimes it is easy and the names come to you when you’re plotting or working out ideas. Sometimes, like with fantasy or science fiction, you want the name to mean something, something representative of your theme or what your character stands for or whatever. And then sometimes you have nothing to go by and nothing magically comes to you and you have to go out searching the web or do what I used to do, which is to consult my book of names I created from the U.S. Census website.
Well, a few weeks ago I gave that book of names to a relative, thinking it was probably time to update it anyway. That’s what lead me to this idea, which is certainly not original but was easy enough to do myself I thought I’d share it with others.
I’m currently working on a series of graphic novels. I’m still in the boring, frustrating, mind-numbing conceptualization stage of the process, unable even to write down the plots of the books with any confidence that I won’t change them drastically when I wake up the next morning. I’m very easily distracted, to say the least, playing a lot of Agent 008 Ball, and wasting valuable brain cells on parts of the story that simply don’t matter that much at this point.
One example is my protagonist’s name. I had been putting off naming my characters because it isn’t important at this point and also because there are so many of them it could take days. But as I was making notes and writing down plot ideas, trying to get an idea of what each book would be about, I grew tired of writing down my protagonist as “the hero” and found myself trying to give him a name.
So the first time I hit a dull part of the process, I did a Google search for “male first names” and the first result took me to this list of the most popular ones at Census.gov. Immediately, I found a few I liked but none of them were good enough. This is my hero character and it’s a series of graphic novels. The name needs to be perfect and it needs to still be perfect 20 years from now when I’m probably still going to be working on this damn project.
This lead me to a search for “popular last names census” and this list of more than 88,000 of them. The idea was that a first name wasn’t enough to satisfy my need for the perfect name and that a magical combination of first and last names would do the trick. Unfortunately, I couldn’t get a good look at both names at the same time. The first names were on one web page in a very long vertical list and the last names were formatted the same way on another page. I needed to get them together, to find a way to see various combinations to pick the ones I liked.
That’s when I thought to make a generator. I’ve been making websites long enough that I’ve needed random generators before, so I knew I could use a few lines of PHP code to randomly pull lines from a text file. I can’t write the code myself, of course, but I’ve accumulated several very useful “snippets” over the years. Granted, I was supposed to be working on plotting my graphic novel series, but I justified it by telling myself how useful such a generator would be and it would only take about 30 minutes to set up anyway.
So that’s what I did, and I figured I’d write about the process here, and also share the web page to anyone who wants to use it. I know now that there are plenty of name generators out there with much better features than mine, but I’m leaving it up anyway for anyone who wants something simple or for anyone who might learn something from it and can apply it to their web design work elsewhere. For instance, I used it once to help my sister generate random Halloween Bingo Cards by simply refreshing the page. A randomization script can be very useful.
Here’s a link to the Random Name Generator… And here’s how I created it.
First, I needed to decide how I wanted the randomly generated names to look on the page. I wanted to fit about fifty or hundred of them into as compact a space as possible so I could skim them quickly before generating some more. It was also important, for the sake of efficiency, to fit both male and female names on the same page. So I opened up Notepad++, the program I use for all of my text editing tasks, and designed the web page using the longest first and last names I could find on the U.S. Census Bureau’s lists. I found the list of female names by doing a Google search for “female first names,” by the way.
Next, I had to recreate the lists of names in a format appropriate for the random generator code. For instance, I knew it worked by pulling each line of text into memory before randomly choosing however many I wanted it to display. Now, PHP is simply a program on your web server that takes its own special language and converts it into to simple HTML. This means that the more lines of text there are in a file, the harder that program has to work and use valuable processing power, and since I use HostGator on a shared server plan, it was crucial to make sure the script could run without bogging down the server and getting my account temporarily suspended if by chance too many surfers happened to be using it at the same time. Basically, this meant I had no choice but to limit the number of lines in my TXT files, even though they were just one-word names.
I decided to go with 1000 names for the male and female TXT files and 5000 for the one with the list of last names. Each time the page would be refreshed it would have to access and read into memory the male names TXT file, the female names TXT file, and the last names TXT file twice. So I decided to be cautious, in other words. I can’t write PHP code to optimize the script and that was plenty of name combinations anyway for my purposes.
So I started the task of creating the TXT files. I copied all the names to new TXT files and saved them as “male.txt,” “female.txt” and “last.txt”. If you’ve looked at the lists at the Census’ website, then you’ll know they have extra data in them to measure popularity. So, since I only needed the names, I did a search on all three files using this regular expression: ^([A-Z]+)([^|]+)$ — and I replaced it with this: \1 — and each line was left with only the names. I also wanted each line to be fifteen characters long, for design purposes, and used a couple of similar searches and replaces to add enough period characters to each line to make up the difference, but I won’t go into that here. After I did another search and replace to get the HTML markup on each line, I was ready to add the PHP code to my web page.
I don’t remember where I found this at — probably a webmaster help forum — but here’s the code:
If you want to use it for your own sites, then there’s a few things you should know. First, be sure to name the extension of your page as PHP rather than HTML because your web host may not have your server set up to parse HTML as PHP. Second, the code is written to pull the TXT file from a location relative to your server root, which is generally named “public_html” and is where your primary TLD is located. The other important setting is the number of items you want to show. Just change the number to whatever you need.
I don’t consider this a serious tutorial and therefore am limiting the amount of information. Though I can’t promise I’m qualified to help, if you need any, you can contact me here.
For Offline Viewing, Download PDF Versions Of All “Just Writing” Posts Here...
Mostly, I just wanted to share how I solved this problem. I love that I can go to my Random Name Generator and just refresh the page until I find a name for my characters that I like. It works even better than I thought it would. Seeing the first names and the last names together was surprisingly more effective than I expected.
Tuesday, January 17th, 2012
Michael Island








