lundi 27 novembre 2017

function for generating no-repeat numbers c#

I try to write a function for generate numbers from 1 to 80 with no-repeat.

the problem is that my generator works incorrectly, because duplicate exists whatever.

public void generator() // сделать по кнопке, но пока что проверка тип на работоспособность 
    {
        Random rand = new Random();

        int[] arr = new int[20];
        int temp = 0;
        foreach (TextBox c in panel1.Controls)
        {
            for (int i = 0; i < 20; i++)
            {
                arr[i] = rand.Next(1, 80);
                temp = arr[i];
                for (int j = 0; j < i; j++)
                {
                    while (arr[i] == arr[j])
                    {
                        arr[i] = rand.Next(1, 80);
                        j = 0;
                        temp = arr[i];
                    }
                    temp = arr[i];
                }
                c.Text = arr[i].ToString();
            }

        }

    }

i tried to use this solution, but i dont understood how to get numbers from List there.

Please, help me




Aucun commentaire:

Enregistrer un commentaire