How to make a random name generator with lists C#

[By JayDev64x] [Index]

Before I showed how to make a random number generator and using the same concept you can use a list and its index number to choose a random part of it. This can be effectively used to create a random name generator or really any word generator of extremly large size.

using System; using System.Collections.Generic; namespace Script { class Example { Random random; void Start() { // Defines a random class to be used to generate the random names and a list to choose the names from. List firstNames = new List() {"Jim", "Bob", "James", "John", "Maria", "Mary", "Robert", "Michael", "Noah", "Elijah"}; List lastNames = new List() {"Smith", "Garcia", "Rodriguez", "Hernandez", "Martinez", "Johnson", "Williams", "Brown", "Davis", "Miller" }; random = new Random(); // The amount of names existent in the list. int totalNames = 10; // Determines the amount of loops the while statement will go through. Console.WriteLine("How many names will be generated?"); int genNum = Convert.ToInt32(Console.ReadLine()); // The loop used to generate each random name. while (genNum > 0) { // Generation and displaying of the random name. int firstGenerated = random.Next(0, totalNames); int lastGenerated = random.Next(0, totalNames); Console.WriteLine("Name: " + firstNames[firstGenerated] + " " + lastNames[lastGenerated]); // This is so the loop will end at one point. genNum -= 1; } Console.WriteLine("Completed generation, press enter to reset."); Console.ReadLine(); Console.Clear(); Start(); } // Takes the program out of a static function. static void Main() { Example example = new Example(); example.Start(); } } }

Created [September 4, 2021]