How to get a random number from a range, excluding some values
Solution 1:
Since no-one has posted any example code:
private int GiveMeANumber()
{
var exclude = new HashSet<int>() { 5, 7, 17, 23 };
var range = Enumerable.Range(1, 100).Where(i => !exclude.Contains(i));
var rand = new System.Random();
int index = rand.Next(0, 100 - exclude.Count);
return range.ElementAt(index);
}
Here's the thinking:
- Build a Hashset of numbers you want to exclude
- Create a collection of all the numbers 0-100 which aren't in your list of numbers to exclude with a bit of LINQ.
- Create a random object.
- Use the Random object to give you a number between 0 and the number of elements in your range of numbers (inclusive).
- Return the number at that index.
Solution 2:
If you care about Big O, check out this algorithm. It assumes that the excluded values array is sorted in ascending order and contains values within 0
and n-1
range (inclusive).
public static int random_except_list(int n, int[] x)
{
Random r = new Random();
int result = r.Next(n - x.Length);
for (int i = 0; i < x.Length; i++)
{
if (result < x[i])
return result;
result++;
}
return result;
}
If you call it with:
random_except_list(8, new int[]{3,4,6})
it will return one of the following values: 0
, 1
, 2
, 5
, 7
.