jeudi 23 mars 2017

C# Random Number Generator with unique numbers

I have managed to make an app which randomly generates a selected amount of numbers and puts all of the numbers in a rich text box (A result I would get from 1-100 with 10 numbers is '67, 55, 28, 35, 7, 69, 47, 59, 69, 82'. However, I am wanting to add an option where you can select if you want the numbers to be unique (checkbox), because the numbers box is close to the max number box, the numbers tend to duplicate.

I am not too sure how to do this though, I have tried looking online but most of the answers are a bit too complicated for me.

The code I have so far (C# Windows Form App FYI):

            int minComplexNumber = Convert.ToInt32(minComplexTextBox.Text);
            int maxComplexNumber = Convert.ToInt32(maxComplexTextBox.Text);
            int intergersNumber = Convert.ToInt32(intergersTextBox.Text);
            int numbersLeft = intergersNumber;
            resultComplexTextBox.Text = "";

            if (UniqueCheckBox.Checked)
            {
                //Need something here
            }
            else
            {
                Random comrnd = new Random();
                while (numbersLeft > 1)
                {

                    int complexResult = comrnd.Next(minComplexNumber, maxComplexNumber);
                    resultComplexTextBox.Text += complexResult + ", ";
                    numbersLeft = numbersLeft - 1;
                }

                if (numbersLeft == 1)
                {
                    int complexResult = comrnd.Next(minComplexNumber, maxComplexNumber);
                    resultComplexTextBox.Text += complexResult;
                    numbersLeft = numbersLeft - 1;
                }
            }

Any advice on how to get unique numbers?




Aucun commentaire:

Enregistrer un commentaire