-1

1.This is what i've got this so far

2.Its suppposed to randomize the numbers on the buttons when it starts. But it still generates duplicates (and i dont want duplicates).

  1. I use Visual studio 2012 C# for this

  2. Its only supposed to do it once i startup the program. So its a one time randomize

                Random rand = new Random();
                int RandomOne = rand.Next(0, 10);
                int RandomTwo = rand.Next(0, 10);
                int RandomThree = rand.Next(0, 10);
                int RandomFour = rand.Next(0, 10);
                int RandomFive = rand.Next(0, 10);
                int RandomSix = rand.Next(0, 10);
                int RandomSeven = rand.Next(0, 10);
                int RandomEight = rand.Next(0, 10);
                int RandomNine = rand.Next(0, 10);
                int RandomZero = rand.Next(0, 10);
    
                btnOneNgra.Text = Convert.ToString(RandomOne);
    
    
                if ((RandomTwo == RandomOne) || (RandomTwo == RandomNine) || (RandomTwo == RandomThree) || (RandomTwo == RandomFour)
                    || (RandomTwo == RandomFive) || (RandomTwo == RandomSix) || (RandomTwo == RandomSeven) || (RandomTwo == RandomEight) || (RandomTwo == RandomZero))
                {
                    RandomTwo = rand.Next(0, 10);
                }
                else 
                {
                    btnTwoNgra.Text = Convert.ToString(RandomTwo);
                }
    
    
                if ((RandomThree == RandomOne) || (RandomThree == RandomTwo) || (RandomThree == RandomNine) || (RandomThree == RandomFour)
                    || (RandomThree == RandomFive) || (RandomThree == RandomSix) || (RandomThree == RandomSeven) || (RandomThree == RandomEight) || (RandomThree == RandomZero))
                {
                    RandomThree = rand.Next(0, 10);
                }
                else
                {
                    btnThreeNgra.Text = Convert.ToString(RandomThree);
                }
    
    
                if ((RandomFour == RandomOne) || (RandomFour == RandomTwo) || (RandomFour == RandomThree) || (RandomFour == RandomNine)
                    || (RandomFour == RandomFive) || (RandomFour == RandomSix) || (RandomFour == RandomSeven) || (RandomFour == RandomEight) || (RandomFour == RandomZero))
                {
                    RandomFour = rand.Next(0, 10);
                }
                else
                {
                    btnFourNgra.Text = Convert.ToString(RandomFour);
                }
    
    
                if ((RandomFive == RandomOne) || (RandomFive == RandomTwo) || (RandomFive == RandomThree) || (RandomFive == RandomFour)
                    || (RandomFive == RandomNine) || (RandomFive == RandomSix) || (RandomFive == RandomSeven) || (RandomFive == RandomEight) || (RandomFive == RandomZero))
                {
                    RandomFive = rand.Next(0, 10);
                }
                else
                {
                    btnFiveNgra.Text = Convert.ToString(RandomFive);
                }
    
    
                if ((RandomSix == RandomOne) || (RandomSix == RandomTwo) || (RandomSix == RandomThree) || (RandomSix == RandomFour)
                    || (RandomSix == RandomFive) || (RandomSix == RandomNine) || (RandomSix == RandomSeven) || (RandomSix == RandomEight) || (RandomSix == RandomZero))
                {
                    RandomSix = rand.Next(0, 10);
                }
                else
                {
                    btnSixNgra.Text = Convert.ToString(RandomSix);
                }
    
    
                if ((RandomSeven == RandomOne) || (RandomSeven == RandomTwo) || (RandomSeven == RandomThree) || (RandomSeven == RandomFour)
                    || (RandomSeven == RandomFive) || (RandomSeven == RandomSix) || (RandomSeven == RandomNine) || (RandomSeven == RandomEight) || (RandomSeven == RandomZero))
                {
                    RandomSeven = rand.Next(0, 10);
                }
                else
                {
                    btnSevenNgra.Text = Convert.ToString(RandomSeven);
                }
    
    
                if ((RandomEight == RandomOne) || (RandomEight == RandomTwo) || (RandomEight == RandomThree) || (RandomEight == RandomFour)
                    || (RandomEight == RandomFive) || (RandomEight == RandomSix) || (RandomEight == RandomSeven) || (RandomEight == RandomNine) || (RandomEight == RandomZero))
                {
                    RandomEight = rand.Next(0, 10);
                }
                else
                {
                    btnEightNgra.Text = Convert.ToString(RandomEight);
                }
    
    
                if ((RandomNine == RandomOne) || (RandomNine == RandomTwo) || (RandomNine == RandomThree) || (RandomNine == RandomFour)
                    || (RandomNine == RandomFive) || (RandomNine == RandomSix) || (RandomNine == RandomSeven) || (RandomNine == RandomEight) || (RandomNine == RandomZero))
                {
                    RandomNine = rand.Next(0, 10);
                }
                else 
                {
                    btnNineNgra.Text = Convert.ToString(RandomNine);
                }
    
    
                if ((RandomZero == RandomOne) || (RandomZero == RandomTwo) || (RandomZero == RandomThree) || (RandomZero == RandomFour)
                    || (RandomZero == RandomFive) || (RandomZero == RandomSix) || (RandomZero == RandomSeven) || (RandomZero == RandomEight) || (RandomZero == RandomNine))
                {
                    RandomZero = rand.Next(0, 10);
                }
                else
                {
                    btnZeroNgra.Text = Convert.ToString(RandomZero);
                }
    
  • 2
    One solution could be to add the numbers(0-9) to a list, shuffle and select a random index, remove element at the index. repeat. EDIT: perhaps select the next index - not a random index after the shuffle. – Faheem May 10 '17 at 12:07
  • You need to do this in a while loop and not a single if-statement. If you check it once and then change it, it may get the same value like another also has. – Daniel Frühauf May 10 '17 at 12:07
  • @Achilles That's of course a way better solution. – Daniel Frühauf May 10 '17 at 12:08
  • Can you give me an example code of how im supposed to do so. Im still a student and need to learn – Noah De Graaff May 10 '17 at 12:11
  • 1
    @NoahDeGraaff If you need to learn I suggest you go try it out. Copy code from SO doesn't teach you anything. – DavidG May 10 '17 at 12:17

2 Answers2

0

The approach for this case should be to create ten numbers in a list and then take one by one to avoid duplication, something like the following:

List<int> nums = new List<int>(Enumerable.Range(0, 10));

private int getNumber(){
    Random rand = new Random();
    int index = rand.Next(0, nums.Count);
    int num = nums[index];
    nums.RemoveAt(index);
    return num;
}

You might need to be careful though, it cannot be called more than 10 times... You might want to create little error handler like:

private int getNumber(){
    if(nums.Count >= 0)
        return -1; //means not available
    Random rand = new Random();
    int index = rand.Next(0, nums.Count);
    int num = nums[index];
    nums.RemoveAt(index);
    return num;
}
Ian
  • 30,182
  • 19
  • 69
  • 107
-1

Maybe you should try with HashSet:

HashSet<int> numbers = new HashSet<int>();
Random randomizer = new Random();

while (numbers.Count < 10)
{
   numbers.Add(randomizer.Next(0, 10));
}

HashSet would make sure that all the values are unique.

  • It would also throw an exception and not give you all the numbers from 0-9 without running it potentially for a very long time (from a probabilistic standpoint) – DavidG May 10 '17 at 12:59
  • It does not throw an exception – gvozdeni88 May 10 '17 at 13:00
  • OK, yes it won't throw an exception but the loop will run dozens of unnecessary times. – DavidG May 10 '17 at 13:02
  • For info, I have tested this a few times. Sometimes it takes less than 20 loops, sometimes it takes over 100. – DavidG May 10 '17 at 13:04