# Working with random numbers and generics

Page 1 of 1

## 0 Replies - 290 Views - Last Post: 25 December 2008 - 09:16 AM

### #1 PsychoCoder

Reputation: 1663
• Posts: 19,853
• Joined: 26-July 07

# Working with random numbers and generics

Posted 25 December 2008 - 09:16 AM

Description: Need a reference to:
System.Collections.GenericsThis is a snippet that takes a min value, max value and how many random numbers from the user then, after it checks to make sure it's not already in the list, adds the new random number to a List<int>
```/// <summary>
/// method for generating a certain amount of random numbers
/// The user provides how many random numbers to generate, and the
/// minimum and maximum values the randon numbers can be
/// </summary>
/// <param name="min">minimum value</param>
/// <param name="max">maximum value</param>
/// <param name="howMany">how many random numbers the user wants</param>
/// <returns></returns>
private static List<int> GenerateRandomNumbers(int minValue, int maxValue, int howMany)
{
//create a generic list of integers
List<int> numberList = new List<int>();

//create an instance of the random class
Random rand = new Random();

//a loop for how many random numbers the user wants
for (int i = 0; i < howMany; i++)
{
int value = rand.Next(minValue, maxValue);
//check and make sure the current
//random number does not exist in our list
if(!(numberList.Contains(value)))
{
//since it doesnt already exist in our list
//we can add it to our list