DEV Community

Oscar Montenegro
Oscar Montenegro

Posted on • Updated on • Originally published at unitcoding.com

Random name generator with C# ๐ŸŽฒ

This is a repost of my article posted on my blog: Unit Coding go and check it out! โœจ

Hello guys, I'm happy to be writing again as it's one of my goals this year to try and have an online presence and acquire new hobbies like blogging which I found attractive since I was young but constancy has never been one of my strengths but I'm giving my best to change that from now on. Ok, that is enough of me now let's get into the main topic a "Random name generator".

Why would I need to generate a name randomly โ“๐Ÿค”

Reasons can be many, for example, imagine an RPG video game if you have ever played you are familiar with the struggle of giving a name to your new character either because you lack the imagination or because the perfect name that you came up with was already taken. In this case, a random names generator comes greatly in handy to take this responsibility (and also struggle) from the player so he can start enjoying the actual game than worrying about thinking of a clever name which can take from some second to several minutes even some hours (gamers know this nightmare ๐Ÿ˜จ)

Create a new console app ๐Ÿ’ป

Let's begin by creating a new console application using the dotnet cli new command.

dotnet new console -o RandomNamesGenerator

Now let's create another class called NamesGenerator.cs here we will start declaring two string arrays called firstNames and lastNames respectively to store our hard-coded list of names and last names.

private static readonly string[] firstNames = {"John","Paul","Ringo","George"};
private static readonly string[] lastNames = {"Lennon","McCartney","Starr","Harrison"};
Enter fullscreen mode Exit fullscreen mode

Time for some randomness! ๐ŸŽฐ

Now below the declaration of the two string arrays let's create a static function called GenerateName which will not receive any parameter and will return a string which will be the random name generated from the names and last names lists.

public static string GenerateName()
{
    var random = new Random();
    string firstName = firstNames[random.Next(0, firstNames.Length)];
    string lastName = firstNames[random.Next(0, firstNames.Length)];

    return $"{firstName} {lastName}";
}
Enter fullscreen mode Exit fullscreen mode

First, we are creating a new instance of the Random class then after this we retrieve a name from the first names array within the range of the array and we do the same for the last name. After this we will concatenate the first and last name using string interpolation and that's it now let's go and test this!

Time for testing ๐Ÿงช

Let's go to the Program.cs file and let's call the GenerateName function from our NamesGenerator class and then all that's left is to print the randomly generated name.

using RandomNames;

var randomName = NamesGenerator.GenerateName();

Console.WriteLine(randomName);
Enter fullscreen mode Exit fullscreen mode

Conclusion ๐Ÿ“

There you go, you can now start enjoying creating any number of creative names expanding and customizing your names array. Also, this example was pretty simple but we could extend this by making use of a random data API to take the names from there and then we could return from one name to a big list of random names and maybe we could receive an input for example if the user wants female names and from a certain ethnicity or fantasy only names, possibilities are endless for this project you just need a bit of creativity and some code.

Follow me on Youtube and support me on Kofi ๐Ÿ’ช๐Ÿฝโ˜•

If you enjoyed this reading please consider following me on youtube and also supporting me on my kofi journey. Thanks and see you on my next article!

unit coding - YouTube

Ko-fi.com - Your Ko-fi

Top comments (0)