How do I make this name generator completely random

-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
c#
visual-studio
asked on Stack Overflow Jul 16, 2020 by clonts • edited Jul 26, 2020 by halfer

1 Answer

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.

answered on Stack Overflow Jul 16, 2020 by madoxdev • edited Jul 16, 2020 by madoxdev

User contributions licensed under CC BY-SA 3.0