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