I'm a completely new to programming and I'm trying to build a stupid name generator and I'm encountering this issue after executing my code a few times.
Exception has occurred: CLR/System.ArgumentOutOfRangeException
Ultimately I'm trying to get it where the user presses enter to generate a new name from a randomly selecting from two separate lists and combining them. This is my code so far:
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
List<string> names1 = new List<string>();
names1.Add("Probiscus");
names1.Add("Feline");
names1.Add("Jerry");
names1.Add("Brock");
names1.Add("Fargoth");
names1.Add("Shoe");
names1.Add("Curly");
names1.Add("Rip");
names1.Add("Spencer");
names1.Add("Fart");
names1.Add("Queefy");
names1.Add("Knifey");
names1.Add("Earl");
names1.Add("Piko");
names1.Add("Juicey");
names1.Add("Snoopy");
names1.Add("Flooper");
names1.Add("Puffin");
names1.Add("Supa");
names1.Add("Spit");
names1.Add("Splicer");
names1.Add("Kiff");
names1.Add("Asmo");
names1.Add("Bimmy");
names1.Add("Boob");
names1.Add("X-Wing");
names1.Add("Goofy");
names1.Add("Smack");
names1.Add("Darth");
names1.Add("Eddy");
List<string> names2 = new List<string>();
names2.Add("Mcadams");
names2.Add("McGoober");
names2.Add("Flynn");
names2.Add("Bickens");
names2.Add("Boofer");
names2.Add("Kilroy");
names2.Add("Spaghet");
names2.Add("Fickinheim");
names2.Add("McJoyless");
names2.Add("Snarker");
names2.Add("Flurmbucket");
names2.Add("Bunny");
names2.Add("Cooper");
names2.Add("Housen");
names2.Add("Galaxo");
names2.Add("Spacoolo");
names2.Add("Aycock");
names2.Add("Burger");
names2.Add("Bonefat");
names2.Add("Cummings");
names2.Add("McNugget");
names2.Add("Spiffmeister");
names2.Add("Turtleman");
names2.Add("Assbucket");
names2.Add("Coughin");
while (Console.ReadKey().Key != ConsoleKey.Escape)
{
var random = new Random();
int index = random.Next(names1.Count);
Console.WriteLine(names1[index] + " " + names2[index]);
Console.ReadKey();
}
}
}
As I've said, after pressing a few keys and the code executes a couple times I run into this error. It does it sometimes after only a few presses or even just one. Any help would be greatly appreciated.