How To Generate Random Numbers In C# Using Random.Next()

This is just a quick basic code snipped someone might find helpful. It generates random numbers between 0, 50. We use a loop to count how many times we cycle, and per each cycle, we increase the rndCount by one. When we reach a certain value (50), we exit the loop.

 public int rndCount { get; private set; }
 private void Button1_Click(object sender, EventArgs e)
 {
 Random rnd = new Random();
 while (rndCount <= 50) //Start a loop
 {
 int newNextRandom = rnd.Next(0, 50); //Between 0 and 50.
 if (rndCount >= 50)
 {
 Console.WriteLine(newNextRandom); 
//Get your new next generated random in the console.
 Console.WriteLine("rndCount has run " + rndCount + " times"); 
//We write the lines per loop how many times rndCount has run.
 rndCount = 0; 
//Reset the counter so you can initiate it again if needed
 break; 
//We have run our course, exit out
 }
 else
 {
 Console.WriteLine(newNextRandom); 
//Get your new next generated random in the console.
 rndCount++; 
//Count how many loops it does approaching 50
 }
 }
 }

The code provided above could obviously be massively overhauled, refactored, and with the implementation of functions, this code could also implement a List<T> to store all the values. Implementing a list could also allow you to check for duplicates and if the value produced by the random.next() already exists in the list, you would request a new random.next() until you accumulate a List<T> collection of 50 values.