-1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RNG
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Adjective f1 = new Adjective();
            textBox1.Text = f1.RandomString();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Made by: Gavin C.\nVersion: 0.01");
        }
    }
}

public class Adjective
{
    List<string> aList = new List<string>
    {
        "Zealous",
        "Bald",
        "Hairless",
        "Bountiful",
        "Cheesy",
        "Crunchy",
        "Hairy",
        "Flaccid",
        "Hard",
        "Large",
        "Small",
        "Massive",
        "Colossal",
        "Dead",
    };
    List<string> nList = new List<string>
    {
        " Sea Horse",
        " Sea Otter",
        " Arctic Wolf",
        " Human",
        " Man",
        " Woman",
        " Horse",
        " Corndog",
        " Hotdog",
        " Chicken",
        " Weiner Dog",
        " Nugget",
        " Chick",
    };
    public string RandomString()
    {
        Random r = new Random();
        int index = r.Next(aList.Count);
        int index2 = r.Next(nList.Count);
        string randomString = aList[index];
        string randomString2 = nList[index];
        return randomString + randomString2;
    }
}

I've been trying to make a program for my friends that randomly generates names. I want to have the adjectives and names randomly selected and returned to the text box on the screen, which I have already done. The issue is it isn't actually random and when you click a bunch of times it crashes the program with this error:

System.ArgumentOutOfRangeException
  HResult=0x80131502
  Message=Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
  Source=mscorlib
  StackTrace:
   at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at Adjective.RandomString() in E:\Programming\Languages\C#\RNG\RNG\Form1.cs:line 84
halfer
  • 19,824
  • 17
  • 99
  • 186
Gavin C.
  • 105
  • 8
  • Duplicate of [Random number generator only generating one random number](https://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number) – 41686d6564 stands w. Palestine Jul 16 '20 at 04:33
  • 1
    And regarding the `ArgumentOutOfRangeException`, most likely `nList[index]` is the cause. You probably intended to use `nList[index2]` instead, right? – 41686d6564 stands w. Palestine Jul 16 '20 at 04:34
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Jul 16 '20 at 10:59

1 Answers1

0

I see two things here:

randomString2 is using index variable that can be outside of array because it's related to count of another array. You probably want to change it to this:

        int index = r.Next(aList.Count);
        int index2 = r.Next(nList.Count);
        string randomString = aList[index];
        string randomString2 = nList[index2];

To get a better random work use a seed passed into Random constructor. Please see this link. For seed you could use that: Guid.NewGuid().GetHashCode().

Random r = new Random(Guid.NewGuid().GetHashCode());

And finally, your class should hold one instance of Random class in the field of the class, so the new instance of the Random class is not generated always when you press a button.

madoxdev
  • 3,770
  • 1
  • 24
  • 39
  • 3
    Please note that the `maxValue` parameter in [`Random.Next(int maxValue)`](https://learn.microsoft.com/en-us/dotnet/api/system.random.next?view=netcore-3.1#System_Random_Next_System_Int32_) is exclusive, _not_ inclusive. So, `.Next(list.Count)` is correct. – 41686d6564 stands w. Palestine Jul 16 '20 at 04:48
  • yeah you're right. I've edited the answer. – madoxdev Jul 16 '20 at 04:50