-1

I Made some Random Int in C#

 Random rnd = new Random();
                int guess = rnd.Next(0, 11); 
                int guess1 = rnd.Next(0, 11);   
                int guess2 = rnd.Next(0, 11);
                int guess3 = rnd.Next(0, 11);

but I don't know what should I do if I want to have not repeat Number ?!?! (Its for guess number game)

Komeil
  • 1
  • possible duplicate of [Generating cryptographic pseudo random numbers](https://stackoverflow.com/a/1668371/7177029) – Kunal Mukherjee Dec 16 '17 at 13:50
  • 2
    What you are looking for is actually something similar to shuffling a deck of cards. In your case the deck contains 11 "cards", `0-10`. Create an array with the numbers `0 through 10`. Then randomly shuffle it (with Fisher-Yates or something similar) and you got 11 guesses in the array and none repeat. – Sani Huttunen Dec 16 '17 at 13:55

1 Answers1

-3

You cannot make it not repeat.

But you can skip/ignore repeated values.

HashSet<int> hs = new HashSet<int>();
while(hs.Count < 5)
    hs.Add(rnd.Next(11));
paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • Downvotes what is the problem? This works. – paparazzo Dec 16 '17 at 14:56
  • I have not downvoted you, but I understand why someone would. The general consensus is that [answering duplicate questions is not advised](https://meta.stackexchange.com/questions/10841/how-should-duplicate-questions-be-handled). – sjaustirni Dec 20 '17 at 15:27
  • @sjaustirni Check the time stamp. It was not a duplicate when I answered. – paparazzo Dec 20 '17 at 15:36
  • It was always a duplicate. It got **marked** as a duplicate 3 minutes after you posted your answer. Besides, 5 minutes *before* your answer was posted Kunal Mukherjee posted a comment suspecting this question to be a duplicate.This should have been enough of a clue for you to be aware of the possibility that this question is a duplicate. – sjaustirni Dec 20 '17 at 15:48
  • 1
    @sjaustirni Moving on, thanks for the input – paparazzo Dec 20 '17 at 16:04