/// <summary>
/// Get Random Numbers without repet
/// </summary>
/// <param name="minRange">Minimum Value</param>
/// <param name="maxRange">Maximum Value</param>
/// <param name="count">Limit Count</param>
/// <returns></returns>
private static List<int> GetRandomNumbers(int minRange, int maxRange, int count)
{
Random randomGenerator = new Random();
List<int> randomList = new List<int>();
for (int i = 0; i < count; i++)
{
var number = randomGenerator.Next(minRange, maxRange);
if (!randomList.Contains(number))
{
randomList.Add(number);
if (randomList.Count>= maxRange)
{
break;
}
}
else
{
i--;
continue;
}
}
return randomList;
}
How to call:
List<int> randomNumbers = GetRandomNumbers(0, 100, 10);
/// Get Random Numbers without repet
/// </summary>
/// <param name="minRange">Minimum Value</param>
/// <param name="maxRange">Maximum Value</param>
/// <param name="count">Limit Count</param>
/// <returns></returns>
private static List<int> GetRandomNumbers(int minRange, int maxRange, int count)
{
Random randomGenerator = new Random();
List<int> randomList = new List<int>();
for (int i = 0; i < count; i++)
{
var number = randomGenerator.Next(minRange, maxRange);
if (!randomList.Contains(number))
{
randomList.Add(number);
if (randomList.Count>= maxRange)
{
break;
}
}
else
{
i--;
continue;
}
}
return randomList;
}
How to call:
List<int> randomNumbers = GetRandomNumbers(0, 100, 10);
No comments:
Post a Comment