0

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.

  • Pl;ease do a thorough search before posting a new question here. A simple search on `ArgumentOutOfRangeException* would have found the duplicate that was linked. A comprehensive effort to search is part of the basic effort to solve the problem yourself before posting that we expect from users here. – Ken White Mar 09 '22 at 01:48
  • `names1` contains 30 items, `names2` contains 25. What do you expect to happen when `names2[index]` requires items 26-30 from `names2`? – ProgrammingLlama Mar 09 '22 at 01:49
  • `names1` has more items than `names2` and the code is getting the `index` from the `names1` list with… `int index = random.Next(names1.Count);` … it is not surprising that some values of `index` will be out of bounds for the `names2` list. Either make both list the same size or generate a separate random index from the `names2` list. – JohnG Mar 09 '22 at 01:50
  • Awesome thanks. I'm pretty stupid so maybe coding isn't for me. – Asmo_Fuhller Mar 09 '22 at 02:04
  • @Asmo_Fuhller It's all about experience. The more problems you tackle, and the more code you write, the more experience you'll build up, and the easier new problems will become to solve. When I started learning programming way back with VB6, I struggled with some really trivial problems (such as removing an item from a list, or converting from a UNIX Epoch timestamp to a date value) which I could do in my sleep now. Don't get disheartened by small problems you have - it's all just a learning experience. :) – ProgrammingLlama Mar 09 '22 at 02:42
  • Thanks for the feedback. Baby steps I guess. – Asmo_Fuhller Mar 09 '22 at 02:47

0 Answers0