-1

I am trying to make a randomizer for my application that has an array of strings and I need the array to get through every string without repeating it again... but, everytime I try, it says StackOverflowException.

private string[] numbers = {
    "1",
    "2",
    "3",
    "4",
    "5",
    "6",
    "7",
    "8",
    "9",
    "10"
};

private string[] previousNumbers = { "", "", "", "", "", "", "", "", "", "" };

private void Randomize(int min, int max)
{
    Random rnd = new Random();
    return rnd.Next(min, max);
}

private string ReturnText()
{
    int num = Randomize(0, 9);
    string number = numbers[num];
    previousNumbers.SetValue(number, num);
    if (number == previousNumbers[num])
    {
        string number_ = numbers[Randomize(0, 9)]
        return number_;
    } else if (number != previousNumbers[num])
        return number;
}

The exception pops up after I try to display the returned text using a button's click event...

  • Include some more details about the exception -- what line is it coming from? What does the stack trace say? Your code has a lot of problems, but there doesn't seem to be anything in the part you have shown that should be causing a stack overflow. Make sure you include *all* of the relevant code as a [mcve]. – Herohtar Apr 09 '20 at 20:33
  • Write the string[] to a List (lstStrings). Then use while loop ( while (lstStrings.Count()) ) and get a random element from the list ( lstString[Randomize(0, lstStrings.Count())] ) and write the returned value to your new string[], then remove that element from your list. You'll end up with a randomly ordered string[]. – Frank Ball Apr 09 '20 at 20:36
  • 1
    Also related, and probably a duplicate: [Best way to randomize an array with .NET](https://stackoverflow.com/questions/108819/best-way-to-randomize-an-array-with-net) – Herohtar Apr 09 '20 at 20:39
  • @Herohtar The StackOverflowException comes from the Randomize class when I attempt multiple randomizations. – sh4d0w4RCH3R415 Apr 17 '20 at 21:09

2 Answers2

0

I have tried to run this code in LINQpad, however there are other errors in this code. I have cleaned up the code below:

private string[] numbers = {
    "1",
    "2",
    "3",
    "4",
    "5",
    "6",
    "7",
    "8",
    "9",
    "10"
};

private string[] previousNumbers = { "", "", "", "", "", "", "", "", "", "" };

private int Randomize(int min, int max)
{
    Random rnd = new Random();
    return rnd.Next(min, max);
}

private string ReturnText()
{
    int num = Randomize(0, 9);
    string number = numbers[num];
    previousNumbers.SetValue(number, num);
    if (number == previousNumbers[num])
    {
        string numberToReturn = numbers[Randomize(0, 9)];
        return numberToReturn;
    }
    else if (number != previousNumbers[num])
    {
        return number;
    }
    else
    {
        return string.Empty;
    }
}

The types of errors in the code seem like this code hasn't been run in visual studio and it's syntax would cause a build failure:

  • Randomise return void when it returns an int.
  • Not all code paths return in ReturnText.
  • Missing semi-colons.

With fixed up code when I call ReturnText() over and over again I get no StackOverflowException.

KingOfArrows
  • 556
  • 2
  • 11
-1

This will set previousNumbers to a randomized string[] of the values in numbers:

        private int Randomize(int max)
        {
            Random rnd = new Random();
            return rnd.Next(0, max);
        }

        private void ReturnText()
        {
            var lstStrings = numbers.ToList<string>();
            int i = 0;
            while (lstStrings.Count() > 0)
            {
                int num = Randomize(lstStrings.Count());
                string number = lstStrings[num];
                previousNumbers[i] = number;                
                lstStrings.Remove(lstStrings[num]);
                i++;
            }           
        }
Frank Ball
  • 1,039
  • 8
  • 15