Random name generator with C#

Random name generator with C#
Name's cards with some markers to the left

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"};
CSharp

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}";
}

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);

Video Version of this Blog

If you enjoy learning more watching than reading, then here's my video version of this article on YouTube.

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