Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A C# helper class for generating random first names and random last names.
using System;
namespace Helpers
{
/// <summary>
/// Name Generator.
/// </summary>
public static class NameGenerator
{
// List of the top 40 boy and girl baby names of 2016
private static readonly string[] FirstNames = { "Liam", "Noah", "Ethan", "Lucas", "Mason", "Oliver", "Aiden", "Elijah", "Benjamin", "James", "Logan", "Jacob", "Jackson", "Michael", "Alexander", "Carter", "Owen", "Daniel", "William", "Luke", "Jack", "Gabriel", "Matthew", "Henry", "Sebastian", "Wyatt", "Nathan", "Isaac", "Grayson", "Ryan", "Caleb", "Levi", "Jayden", "Jaxon", "Eli", "David", "Dylan", "Andrew", "Julian", "Samuel", "Emma", "Olivia", "Ava", "Sophia", "Isabella", "Mia", "Charlotte", "Harper", "Amelia", "Emily", "Abigail", "Madison", "Ella", "Lily", "Avery", "Evelyn", "Sofia", "Aria", "Chloe", "Riley", "Ellie", "Scarlett", "Elizabeth", "Aubrey", "Layla", "Addison", "Grace", "Zoey", "Hannah", "Mila", "Zoe", "Victoria", "Brooklyn", "Lucy", "Penelope", "Natalie", "Nora", "Audrey", "Skylar", "Claire" };
// List of the most common 80 USA last names
private static readonly string[] LastNames = { "Smith", "Johnson", "Williams", "Brown", "Jones", "Miller", "Davis", "Garcia", "Rodriguez", "Wilson", "Martinez", "Anderson", "Taylor", "Thomas", "Hernandez", "Moore", "Martin", "Jackson", "Thompson", "White", "Lopez", "Lee", "Gonzalez", "Harris", "Clark", "Lewis", "Robinson", "Walker", "Perez", "Hall", "Young", "Allen", "Sanchez", "Wright", "King", "Scott", "Green", "Baker", "Adams", "Nelson", "Hill", "Ramirez", "Campbell", "Mitchell", "Roberts", "Carter", "Phillips", "Evans", "Turner", "Torres", "Parker", "Collins", "Edwards", "Stewart", "Flores", "Morris", "Nguyen", "Murphy", "Rivera", "Cook", "Rogers", "Morgan", "Peterson", "Cooper", "Reed", "Bailey", "Bell", "Gomez", "Kelly", "Howard", "Ward", "Cox", "Diaz", "Richardson", "Wood", "Watson", "Brooks", "Bennett", "Gray", "James" };
/// <summary>
/// Generates a random first name.
/// </summary>
/// <returns>First name.</returns>
public static string GenerateFirstName()
{
return GetRandomArrayEntry(FirstNames);
}
/// <summary>
/// Generates a random last name.
/// </summary>
/// <returns>Last name.</returns>
public static string GenerateLastName()
{
return GetRandomArrayEntry(LastNames);
}
/// <summary>
/// Gets a random array entry.
/// </summary>
/// <typeparam name="T">Array type.</typeparam>
/// <param name="array">Array of type T.</param>
/// <returns>Array entry.</returns>
private static T GetRandomArrayEntry<T>(T[] array)
{
return array[new Random(Guid.NewGuid().GetHashCode()).Next(0, array.Length)];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment