jeudi 25 octobre 2018

C# - Duplicate Random Numbers are Getting Displayed

I have written this very crude way of generating random numbers and displaying in a labelText inside a Windows Form. What is happening is that for the second label text it is showing exactly the same of the first label text. Hoping for your feedback on how to correct this so that each label text displays unique random numbers.

    private void btnGenerateNumbers_Click(object sender, EventArgs e)
    {
        List<string> numberStringT = new List<string>();


        for (int i = 0; i < 2; i++)
        {
            string result = "";
            Random rnd = new Random();
            int one = rnd.Next(1, 49);
            int two = rnd.Next(1, 49);
            int three = rnd.Next(1, 49);
            int four = rnd.Next(1, 49);
            int five = rnd.Next(1, 49);
            int six = rnd.Next(1, 49);
            int seven = rnd.Next(1, 49);

            if ((one == two) | (one == three) | (one == four) | (one == five) | (one == six) | (one == seven))
            {
                one = rnd.Next(1, 49);
            }

            if ((two == one) | (two == three) | (two == four) | (two == five) | (two == six) | (two == seven))
            {
                two = rnd.Next(1, 49);
            }

            if ((three == one) | (three == two) | (three == four) | (three == five) | (three == six) | (three == seven))
            {
                three = rnd.Next(1, 49);
            }

            if ((four == one) | (four == two) | (four == three) | (four == five) | (four == six) | (four == seven))
            {
                four = rnd.Next(1, 49);
            }

            if ((five == one) | (five == two) | (five == three) | (five == four) | (five == six) | (five == seven))
            {
                five = rnd.Next(1, 49);
            }

            if ((six == one) | (six == two) | (six == three) | (six == four) | (six == five) | (six == seven))
            {
                six = rnd.Next(1, 49);
            }

            if ((seven == one) | (seven == two) | (seven == three) | (seven == four) | (seven == five) | (seven == six))
            {
                seven = rnd.Next(1, 49);
            }


            List<int> numberList = new List<int>();

            List<int> numberListNoDuplicates = new List<int>();


            numberList.Add(one);
            numberList.Add(two);
            numberList.Add(three);
            numberList.Add(four);
            numberList.Add(five);
            numberList.Add(six);
            numberList.Add(seven);

            numberList.Sort();


            result = numberList[0].ToString() + "  " + numberList[1].ToString() + "  " + numberList[2].ToString() + "  " + numberList[3].ToString() + "  " + numberList[4].ToString() + "  " + numberList[5].ToString() + "  " + numberList[6].ToString();
            numberStringT.Add(result);
            numberList.Clear();
            result = "";
            rnd.Next();
        }


        //lblRandomNumber.Text = result;
        lblRandomNumber.Text = numberStringT[0];
        lblRandomNumber2.Text = numberStringT[1];
    }




Aucun commentaire:

Enregistrer un commentaire