How to get a random number from a range, excluding some values

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:

  1. Build a Hashset of numbers you want to exclude
  2. Create a collection of all the numbers 0-100 which aren’t in your list of numbers to exclude with a bit of LINQ.
  3. Create a random object.
  4. Use the Random object to give you a number between 0 and the number of elements in your range of numbers (inclusive).
  5. Return the number at that index.

Leave a Comment