How to make a random name generator in C# How to make a random name generator in C#

How to make a random name generator in C#

I recnetly came up with a simple random name generator to do an initial seed of a database for testing using real names,a nd I thought I would share. Here is my NameGenerator.cs file. It’s fairly simple, it just picks a name at random out of the top 100 first names and the top 100 last names for the year 2011. Enjoy!

The code for the random name generator in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyNamespace
{
public class NameGenerator
{
public static Random rnd = new Random();

 

public static string GenRandomLastName()
{
List<string> lst = new List<string>();
string str = string.Empty;
lst.Add(“Smith”);
lst.Add(“Johnson”);
lst.Add(“Williams”);
lst.Add(“Jones”);
lst.Add(“Brown”);
lst.Add(“Davis”);
lst.Add(“Miller”);
lst.Add(“Wilson”);
lst.Add(“Moore”);
lst.Add(“Taylor”);
lst.Add(“Anderson”);
lst.Add(“Thomas”);
lst.Add(“Jackson”);
lst.Add(“White”);
lst.Add(“Harris”);
lst.Add(“Martin”);
lst.Add(“Thompson”);
lst.Add(“Garcia”);
lst.Add(“Martinez”);
lst.Add(“Robinson”);
lst.Add(“Clark”);
lst.Add(“Rodriguez”);
lst.Add(“Lewis”);
lst.Add(“Lee”);
lst.Add(“Walker”);
lst.Add(“Hall”);
lst.Add(“Allen”);
lst.Add(“Young”);
lst.Add(“Hernandez”);
lst.Add(“King”);
lst.Add(“Wright”);
lst.Add(“Lopez”);
lst.Add(“Hill”);
lst.Add(“Scott”);
lst.Add(“Green”);
lst.Add(“Adams”);
lst.Add(“Baker”);
lst.Add(“Gonzalez”);
lst.Add(“Nelson”);
lst.Add(“Carter”);
lst.Add(“Mitchell”);
lst.Add(“Perez”);
lst.Add(“Roberts”);
lst.Add(“Turner”);
lst.Add(“Phillips”);
lst.Add(“Campbell”);
lst.Add(“Parker”);
lst.Add(“Evans”);
lst.Add(“Edwards”);
lst.Add(“Collins”);
lst.Add(“Stewart”);
lst.Add(“Sanchez”);
lst.Add(“Morris”);
lst.Add(“Rogers”);
lst.Add(“Reed”);
lst.Add(“Cook”);
lst.Add(“Morgan”);
lst.Add(“Bell”);
lst.Add(“Murphy”);
lst.Add(“Bailey”);
lst.Add(“Rivera”);
lst.Add(“Cooper”);
lst.Add(“Richardson”);
lst.Add(“Cox”);
lst.Add(“Howard”);
lst.Add(“Ward”);
lst.Add(“Torres”);
lst.Add(“Peterson”);
lst.Add(“Gray”);
lst.Add(“Ramirez”);
lst.Add(“James”);
lst.Add(“Watson”);
lst.Add(“Brooks”);
lst.Add(“Kelly”);
lst.Add(“Sanders”);
lst.Add(“Price”);
lst.Add(“Bennett”);
lst.Add(“Wood”);
lst.Add(“Barnes”);
lst.Add(“Ross”);
lst.Add(“Henderson”);
lst.Add(“Coleman”);
lst.Add(“Jenkins”);
lst.Add(“Perry”);
lst.Add(“Powell”);
lst.Add(“Long”);
lst.Add(“Patterson”);
lst.Add(“Hughes”);
lst.Add(“Flores”);
lst.Add(“Washington”);
lst.Add(“Butler”);
lst.Add(“Simmons”);
lst.Add(“Foster”);
lst.Add(“Gonzales”);
lst.Add(“Bryant”);
lst.Add(“Alexander”);
lst.Add(“Russell”);
lst.Add(“Griffin”);
lst.Add(“Diaz”);
lst.Add(“Hayes”);

str = lst.OrderBy(xx => rnd.Next()).First();
return str;
}
public static string GenRandomFirstName()
{
List<string> lst = new List<string>();
string str = string.Empty;
lst.Add(“Aiden”);
lst.Add(“Jackson”);
lst.Add(“Mason”);
lst.Add(“Liam”);
lst.Add(“Jacob”);
lst.Add(“Jayden”);
lst.Add(“Ethan”);
lst.Add(“Noah”);
lst.Add(“Lucas”);
lst.Add(“Logan”);
lst.Add(“Caleb”);
lst.Add(“Caden”);
lst.Add(“Jack”);
lst.Add(“Ryan”);
lst.Add(“Connor”);
lst.Add(“Michael”);
lst.Add(“Elijah”);
lst.Add(“Brayden”);
lst.Add(“Benjamin”);
lst.Add(“Nicholas”);
lst.Add(“Alexander”);
lst.Add(“William”);
lst.Add(“Matthew”);
lst.Add(“James”);
lst.Add(“Landon”);
lst.Add(“Nathan”);
lst.Add(“Dylan”);
lst.Add(“Evan”);
lst.Add(“Luke”);
lst.Add(“Andrew”);
lst.Add(“Gabriel”);
lst.Add(“Gavin”);
lst.Add(“Joshua”);
lst.Add(“Owen”);
lst.Add(“Daniel”);
lst.Add(“Carter”);
lst.Add(“Tyler”);
lst.Add(“Cameron”);
lst.Add(“Christian”);
lst.Add(“Wyatt”);
lst.Add(“Henry”);
lst.Add(“Eli”);
lst.Add(“Joseph”);
lst.Add(“Max”);
lst.Add(“Isaac”);
lst.Add(“Samuel”);
lst.Add(“Anthony”);
lst.Add(“Grayson”);
lst.Add(“Zachary”);
lst.Add(“David”);
lst.Add(“Christopher”);
lst.Add(“John”);
lst.Add(“Isaiah”);
lst.Add(“Levi”);
lst.Add(“Jonathan”);
lst.Add(“Oliver”);
lst.Add(“Chase”);
lst.Add(“Cooper”);
lst.Add(“Tristan”);
lst.Add(“Colton”);
lst.Add(“Austin”);
lst.Add(“Colin”);
lst.Add(“Charlie”);
lst.Add(“Dominic”);
lst.Add(“Parker”);
lst.Add(“Hunter”);
lst.Add(“Thomas”);
lst.Add(“Alex”);
lst.Add(“Ian”);
lst.Add(“Jordan”);
lst.Add(“Cole”);
lst.Add(“Julian”);
lst.Add(“Aaron”);
lst.Add(“Carson”);
lst.Add(“Miles”);
lst.Add(“Blake”);
lst.Add(“Brody”);
lst.Add(“Adam”);
lst.Add(“Sebastian”);
lst.Add(“Adrian”);
lst.Add(“Nolan”);
lst.Add(“Sean”);
lst.Add(“Riley”);
lst.Add(“Bentley”);
lst.Add(“Xavier”);
lst.Add(“Hayden”);
lst.Add(“Jeremiah”);
lst.Add(“Jason”);
lst.Add(“Jake”);
lst.Add(“Asher”);
lst.Add(“Micah”);
lst.Add(“Jace”);
lst.Add(“Brandon”);
lst.Add(“Josiah”);
lst.Add(“Hudson”);
lst.Add(“Nathaniel”);
lst.Add(“Bryson”);
lst.Add(“Ryder”);
lst.Add(“Justin”);
lst.Add(“Bryce”);

//—————female

lst.Add(“Sophia”);
lst.Add(“Emma”);
lst.Add(“Isabella”);
lst.Add(“Olivia”);
lst.Add(“Ava”);
lst.Add(“Lily”);
lst.Add(“Chloe”);
lst.Add(“Madison”);
lst.Add(“Emily”);
lst.Add(“Abigail”);
lst.Add(“Addison”);
lst.Add(“Mia”);
lst.Add(“Madelyn”);
lst.Add(“Ella”);
lst.Add(“Hailey”);
lst.Add(“Kaylee”);
lst.Add(“Avery”);
lst.Add(“Kaitlyn”);
lst.Add(“Riley”);
lst.Add(“Aubrey”);
lst.Add(“Brooklyn”);
lst.Add(“Peyton”);
lst.Add(“Layla”);
lst.Add(“Hannah”);
lst.Add(“Charlotte”);
lst.Add(“Bella”);
lst.Add(“Natalie”);
lst.Add(“Sarah”);
lst.Add(“Grace”);
lst.Add(“Amelia”);
lst.Add(“Kylie”);
lst.Add(“Arianna”);
lst.Add(“Anna”);
lst.Add(“Elizabeth”);
lst.Add(“Sophie”);
lst.Add(“Claire”);
lst.Add(“Lila”);
lst.Add(“Aaliyah”);
lst.Add(“Gabriella”);
lst.Add(“Elise”);
lst.Add(“Lillian”);
lst.Add(“Samantha”);
lst.Add(“Makayla”);
lst.Add(“Audrey”);
lst.Add(“Alyssa”);
lst.Add(“Ellie”);
lst.Add(“Alexis”);
lst.Add(“Isabelle”);
lst.Add(“Savannah”);
lst.Add(“Evelyn”);
lst.Add(“Leah”);
lst.Add(“Keira”);
lst.Add(“Allison”);
lst.Add(“Maya”);
lst.Add(“Lucy”);
lst.Add(“Sydney”);
lst.Add(“Taylor”);
lst.Add(“Molly”);
lst.Add(“Lauren”);
lst.Add(“Harper”);
lst.Add(“Scarlett”);
lst.Add(“Brianna”);
lst.Add(“Victoria”);
lst.Add(“Liliana”);
lst.Add(“Aria”);
lst.Add(“Kayla”);
lst.Add(“Annabelle”);
lst.Add(“Gianna”);
lst.Add(“Kennedy”);
lst.Add(“Stella”);
lst.Add(“Reagan”);
lst.Add(“Julia”);
lst.Add(“Bailey”);
lst.Add(“Alexandra”);
lst.Add(“Jordyn”);
lst.Add(“Nora”);
lst.Add(“Carolin”);
lst.Add(“Mackenzie”);
lst.Add(“Jasmine”);
lst.Add(“Jocelyn”);
lst.Add(“Kendall”);
lst.Add(“Morgan”);
lst.Add(“Nevaeh”);
lst.Add(“Maria”);
lst.Add(“Eva”);
lst.Add(“Juliana”);
lst.Add(“Abby”);
lst.Add(“Alexa”);
lst.Add(“Summer”);
lst.Add(“Brooke”);
lst.Add(“Penelope”);
lst.Add(“Violet”);
lst.Add(“Kate”);
lst.Add(“Hadley”);
lst.Add(“Ashlyn”);
lst.Add(“Sadie”);
lst.Add(“Paige”);
lst.Add(“Katherine”);
lst.Add(“Sienna”);
lst.Add(“Piper”);

str = lst.OrderBy(xx => rnd.Next()).First();
return str;
}
}
}

 

Written By Steve French

 

3 responses to “How to make a random name generator in C#”

  1. Terribly inefficient code. You create the list every call, then tell .Net to reorder the entire list just to produce one random record from a constant list. MUCH better would be to make two static list fields in your NameGenerator class, populate them in the static constructor, and when a random name is requested, randomly produce an index into the list and return the name at that index.

    This is not a trivial improvement either. I tested your code and my code at 100, 1000, 10000, and 100000 name pairs (first and last), and the difference is significant. At 100, there are about equal. At 1000, mine was 4 times faster. At 10000, mine was about 24 times faster. At 100000, mine was over 190 times faster.

    Simplicity is fine, but performance is better. And in this case, there’s absolutely no trade-off in readability for optimizing, so you should just go ahead and do it.

    • Thank you for your comments!

      And yes, there are definitely better ways to do it, the method you suggested being the best. If I revise it, I’ll post it here, but for a small internal tool I think it works well enough, for my purposes anyway.

      All I’m doing with it is some initial database population every couple days so it is not a big deal if that part takes a few extra seconds.

      Thank you for your input though, I would not have guessed that the differences would be that extreme.

  2. Hey man! This is great! Thanks so much for posting this, i am making a text based rpg in a console app and when generating random characters i needed to get a list of random names to give them. This fits both of my needs, thanks for posting your hard work for others to use, this just saved me alot of time 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *






Copyright 2011 Digital Tool Factory. All Rights Reserved. Powered by raw technical talent. And in this case, WordPress.