0

I have this code that shuffle numbers from 1 to 17 and put these numbers into textboxes
The problem occurs when using While loop the form doesn't appear and the calculations take a lot of time what is the wrong ??

this is the code that I wrote :

    private void Level_1_Load(object sender, EventArgs e)
    {

        shuffle();
    }

    private void shuffle()
    {
        Random rand = new Random();
        int randomNum;
        int[] NumArr = new int[17];
        int counter = 0;

        bool IsDuplicate = false;

        do
        {
            randomNum = rand.Next(17);
            IsDuplicate = false;

            for (int i = 0; i < NumArr.Length; i++)
            {
                if (NumArr[i] == randomNum)
                {
                    IsDuplicate = true; break;
                }
            }
            if (IsDuplicate == false)
            {
                NumArr[counter] = randomNum;
                counter++;
            }

        } while (counter < 17);


    TextBox1.Text = NumArr[0].ToString();
    TextBox2.Text = NumArr[1].ToString();
    TextBox3.Text = NumArr[2].ToString();
    TextBox4.Text = NumArr[3].ToString();
    TextBox5.Text = NumArr[4].ToString();
    TextBox6.Text = NumArr[5].ToString();
    TextBox7.Text = NumArr[6].ToString();
    TextBox8.Text = NumArr[7].ToString();
    TextBox9.Text = NumArr[8].ToString();
    TextBox10.Text = NumArr[9].ToString();
    TextBox11.Text = NumArr[10].ToString();
    TextBox12.Text = NumArr[11].ToString();
    TextBox13.Text = NumArr[12].ToString();
    TextBox14.Text = NumArr[13].ToString();
    TextBox15.Text = NumArr[14].ToString();
    TextBox16.Text = NumArr[15].ToString();
    TextBox17.Text = NumArr[16].ToString();

    }
  • Use a [random sort algorithm](https://www.dotnetperls.com/fisher-yates-shuffle) – stuartd Nov 17 '19 at 13:43
  • It seems it will never gonna break the loop. Because your counter never gonna be higher then 16. After it reached to 16 it wont get increased because duplicate will not false again since array is full with values. – Eldar Nov 17 '19 at 13:45
  • [Best way to randomize an array with .NET](https://stackoverflow.com/q/108819/215552) – Heretic Monkey Nov 17 '19 at 14:20
  • Does this answer your question? [Best way to randomize an array with .NET](https://stackoverflow.com/questions/108819/best-way-to-randomize-an-array-with-net) – Heretic Monkey Nov 17 '19 at 14:21

2 Answers2

1

It's probably best that you initialize your array with the numbers you want and then shuffle them around. Something like:

using System;

public class Program
{
    public static void Main()
    {
        int[] numbers = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
        shuffle(numbers);
        Console.WriteLine(String.Join(", ", numbers));
    }

    private static void shuffle(int[] numbers)
    {
        Random r = new Random((int)DateTime.Now.Ticks);
        for (int i = 0; i < numbers.Length; i++)
        {
            int temp = numbers[i];
            int index = r.Next(numbers.Length);
            numbers[i] = numbers[index];
            numbers[index] = temp;
        }
    }
}

RESULT (will be different on each run)

9, 16, 12, 6, 3, 5, 4, 14, 15, 11, 8, 10, 17, 1, 2, 13, 7

Fiddle Demo

Shar1er80
  • 9,001
  • 2
  • 20
  • 29
0

You probably want to use another algorithm for filling an array with non-duplicate numbers.

For example, initialize your array with [0, 1, 2, 3, 4...] and shuffle it.

Tho Mai
  • 835
  • 5
  • 25
  • how can i shuffle the array without using random class ?? – Silver Hawk Nov 17 '19 at 14:00
  • Why don't you want to use the random class? To shuffle an array, iterate from 0 to 16, pick an random number between [0,16] and change the current array position with the random number's index. – Tho Mai Nov 17 '19 at 14:11