title: How can I ensure that same random numbers are not selected every time in C#?
body: <p>I'm writing a trivia game player app in C#, where the user must pick 5 randomly selected trivia questions from a set of 20 available questions.</p>
  
  <p>I use this code to generate an index that the app will use to fetch the questions:</p>
  
  <pre><code>    private void GenerateRandomNumbers(int RandomIndex)
      {
          Random rand = new Random();
          HashSet&lt;int&gt; check = new HashSet&lt;int&gt;();
          for (int i = 1; i &lt; RandomIndex; i++)
          {
              int curValue = rand.Next(1, RandomIndex);
              while (check.Contains(curValue))
              {
                  curValue = rand.Next(1, RandomIndex);
              }
              ChosenQuestions.Add(Questions[curValue]);
  
              check.Add(curValue);
          }
          check.Clear();
      }
  </code></pre>
  
  <p>The problem is that when the user plays the game a second time (by clicking the "PLAY AGAIN" button) and the above code is executed again, it keeps picking the same random numbers, though they are not necessarily in the same order. this causes the same trivia questions to be chosen every time. I want a fresh new set of totally random numbers every time the above code executes. How can I modify the code to accomplish this?</p>
  
  <p>NOTE: I did copy this code from somewhere else here on Stack Overflow, so it's not my own original code.</p>