Click here to Skip to main content
Click here to Skip to main content

Creating a Nickname Generator

By , 19 Aug 2007
Rate this:
Please Sign up or sign in to vote.
Screenshot - nickgen-screen.png

Introduction

In this article, I will guide you in creation of a nickname generator that will allow the user to generate a nickname of arbitrary length. It will also offer a possibility to look up the generated nickname at Google to see if it has any Google search result. If the nickname has 0 results, it should be unique.

We often register ourselves at message boards and various sites, but we don't always want to use our real name or "official" nickname. Sometimes it's desirable to have a unique nickname that can't be traced back to us. There are more reasons for this - either to do some undercover work, or to discuss some private matters anonymously. This is the occasion where a nickname generator comes in handy.

The easiest way to generate a nickname would seem to be to generate a random string. But the result would look like tmxoit, rdyari, qefoauh, uatyni - not too good. It really seems like a random bunch of characters and might be hard to memorize.

A good way to generate a nickname might be to alternate vowels and consonants, so we will get results like Zobona, Osavir, Ecumol, or Obunu.

Implementation

Nickname generation will be encapsulated in a class with the following class diagram:

That class would contain arrays of all available vowels and consonants:

char[] vowels = new char[] { 'a', 'e', 'i', 'o', 'u', 'y' }; 
char[] consonants = new char[] { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 
    'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'z' }; 

The constructor of class NickGenerator initializes the random number generator.

The class contains one public method Generate(int length) that returns a string containing the generated nickname of supplied length.

public string Generate(int length)
{
    StringBuilder sb = new StringBuilder();
    //initialize our vowel/consonant flag
    bool flag = (random.Next(2) == 0);
    for (int i = 0; i < length; i++)
    {
        sb.Append(GetChar(flag));
        flag = !flag; //invert the vowel/consonant flag
    }
    return sb.ToString();
}

It first decides if the starting character would be a vowel or a consonant, then in a loop adds other vowels and consonants (in turns) to the result.

The method that gets the random character is as follows:

private char GetChar(bool vowel)
{
    if (vowel)
    {
        return vowels[random.Next(vowels.Length)];
    }
    return consonants[random.Next(consonants.Length)];
}

The final step would be to check if the generated nickname is unique. The class provides a method GetWebResultsForNickname for this purpose. The method has the following body:

public static int GetWebResultsForNickname(string text)
{
    string address = http://www.google.com/xhtml/search?mrestrict=xhtml&hl=en&q= 
                + text;
    WebClient client = new WebClient();
    client.Encoding = Encoding.UTF8;
    string input = client.DownloadString(address);
    //regex 
    Match match = new Regex
        ("Results [0-9] - [0-9]+ of .*?([0-9|,]+)\\.").Match(input);
    if (match.Success)
    {
        return int.Parse(match.Groups[1].Value.Replace(",", ""));
    }
    return 0;
}

The method connects to Google's server and retrieves a result page for our nickname. Then we check with regular expression "Results [0-9] - [0-9]+ of .*?([0-9|,]+)\\." for the number of results. They are displayed in two forms: Results 0 - 3 of 3 or Results 0 - 10 of about 123,456. The number of results has the thousands separator "," - we deal with that in number parsing. An alternate way to parse a number of that format would be to use the NumberFormatInfo class.

For the sake of simplicity, the code is not user-proof and could crash under exceptional conditions (e.g. negative length of nickname).

Demo Program

The demo program consists of a simple Windows Form, and it uses this class to generate the nickname. The length of the result can be chosen from the NumericUpDown control.

Further Steps

You can try to improve this nickname generator to yield more realistic results. There is definitely room for improvement, because in human languages, some characters tend to be grouped with some others more often, and some groupings almost never occur (for example yqi, etc.). Also, some characters should be less likely to be the first character of a generated nickname (results that begin with q and y sound strange to me most of the time).

I think that you can easily enhance results by adding some extra logic to the method that generates the nickname - like add some factors to each character that will specify how likely it is to appear and also how likely it is so the resulting word will begin or start with it.

Further improvements - to make the results look like human language words - would possibly require knowledge of linguistics, which I am unfortunately no expert at.

History

  • 19th August, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Juraj Borza
Software Developer MicroStep
Slovakia Slovakia
Juraj Borza is currently working as a full-time C# developer located in Bratislava, Slovakia. He likes coding for fun and releases some projects under open-source license.

Comments and Discussions

 
GeneralGood stuff to generate demodata :-) PinmemberNickolay Karnaukhov29-Aug-07 2:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 20 Aug 2007
Article Copyright 2007 by Juraj Borza
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid