Select from a range but exclude certain numbers [d

2019-06-15 23:50发布

This question already has an answer here:

Is it possible to pick a random number from a given range (1-90), but exclude certain numbers. The excluded numbers are dynamically created but lets say they are 3, 8, and 80.

I have managed to create random number generator but couldn't identify any functions that let me fulfill my requirements.

Random r = new Random();
this.num = r.Next(1, 90);

The numbers which are to be excluded are previously generated numbers. So, if the random number is one, this would then get added to the excluded numbers list.

7条回答
我只想做你的唯一
2楼-- · 2019-06-16 00:11
Random r = new Random();
this.num = r.Next(1, 90);

int excluded[] = new int[] { 3,8,80 }; // list any numbers in this array you want to exclude

for (int i = 0; i < excluded.Length; i++)
{
    if (this.num == excluded[i])
    {
        this.num = r.Next(1, 90); // or you can try doing something else here
    }
}
查看更多
劫难
3楼-- · 2019-06-16 00:13

Your latest update, which implies that each value can only be selected once, makes the problem easy.

  1. Create a collection of values within the range.
  2. Randomly shuffle the collection.
  3. To"randomly" select an item, just return the first item in the collection, and remove it from the collection.
查看更多
地球回转人心会变
4楼-- · 2019-06-16 00:16

Using some handy extension methods here, you can create a range of numbers and select randomly from that rage. For example, with these extension methods:

public static T RandomElement(this IEnumerable<T> enumerable)
{
    return enumerable.RandomElementUsing(new Random());
}

public static T RandomElementUsing(this IEnumerable<T> enumerable, Random rand)
{
    int index = rand.Next(0, enumerable.Count());
    return enumerable.ElementAt(index);
}

You can apply these to a filtered range of numbers:

var random = Enumerable.Range(1, 90).Except(arrayOfRemovedNumbers).RandomElement();
查看更多
叛逆
5楼-- · 2019-06-16 00:26

Create a container which holds the numbers you do not want:

var excludedNumbers = new List<int> { 1, 15, 35, 89 };

Then use do something like:

Random random = new Random();

int number;

do
{
   number = r.Next(1, 90);
} while (excludedNumbers.Contains(number));

// number is not in the excluded list now
查看更多
Explosion°爆炸
6楼-- · 2019-06-16 00:26

Make sure excludedNumbers is a HashSet for best performance.

var random = new Random();
var exludedNumbers = new HashSet<int>(new int[] { 3, 8, 80});
var randomNumber = (from n in Enumerable.Range(int.MinValue, int.MaxValue)
                    let number = random.Next(1, 90)
                    where !exludedNumbers.Contains(number)
                    select number).First();
查看更多
淡お忘
7楼-- · 2019-06-16 00:32

Might not be the best choice but you can use a while loop to check the numbers you don't want

Random r = new Random();
this.num = r.Next(1, 90);
do
{
    this.num = r.Next(1, 90);
}  
while (this.num == 3 || this.num == 8 || this.num == 90);

For much numbers you can use an array or a list and loop through them

int[] exclude = { 3, 8, 90, 11, 24 };
Random r = new Random();
this.num = r.Next(1, 90);
do
{
    this.num = r.Next(1, 90);
}
while (exclude.Contains(this.num));
查看更多
登录 后发表回答