How to Get a Random Number from a Range, Excluding Some Values

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.

Generate random number in range excluding some numbers

Try this:

from random import choice

print(choice([i for i in range(0,9) if i not in [2,5,7]]))

Exclude values from Random.Range()?

The best way to do this is to use your favourite generator to generate an integer n between 1 and 17 then transform using

if (n > 5){
n += 3;
}

If you sample between 1 and 20 then discard values you can introduce statistical anomalies, particularly with low discrepancy sequences.

Random integer in a certain range excluding one number

The fastest way to obtain a random integer number in a certain range [a, b], excluding one value c, is to generate it between a and b-1, and then increment it by one if it's higher than or equal to c.

Here's a working function:

function randomExcluded(min, max, excluded) {
var n = Math.floor(Math.random() * (max-min) + min);
if (n >= excluded) n++;
return n;
}

This solution only has a complexity of O(1).

How do you create a random list of integers, but exclude a certain number?

Make a list of the numbers in the range, and remove k from the list. Then you can use random.choices() to select multiple elements from the list.

numbers = list(range(0, 11))
numbers.remove(k)
result = random.choices(numbers, k=5)

How can I generate a random number within a range but exclude some?

Set an array with all the values (this is only a valid option if you're only doing small numbers, like the 25 in your example), like this:

var array = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24];

then, pick a random number between 0 and the array length:

var num = Math.floor(Math.random() * array.length);

remove that index number from the array:

var roll = array.splice(num, 1);

Javascript splice() removes indexed items from an array and returns the item(s) as an array. Perfect for your use.

Grab the first index from the roll, since we only cut 1 out anyway:

var yourNumber = roll[ 0 ];

Keep doing for as many rolls as you want. Also, you might want to store the original array as a copy so that you can "reset" the numbers easily.

How can I generate a random number within a range but exclude some?

One possible solution without regeneration the random each time is to use the following algorithm:

public int getRandomWithExclusion(Random rnd, int start, int end, int... exclude) {
int random = start + rnd.nextInt(end - start + 1 - exclude.length);
for (int ex : exclude) {
if (random < ex) {
break;
}
random++;
}
return random;
}

This method can be either called with an array reference, e.g.

int[] ex = { 2, 5, 6 };
val = getRandomWithExclusion(rnd, 1, 10, ex)

or by directly inserting the numbers into the call:

val = getRandomWithExclusion(rnd, 1, 10, 2, 5, 6)

It generates a random number (int) between start and end (both inclusive) and does not give you any number which is contained in the array exclude. All other numbers occur with equal probability. Note, that the following constrains must hold: exclude is sorted ascendingly and all numbers are within the range provided and all of them are mutually different.

Return random number excluding specific range - javascript

One way to handle this is to look for a random number in the range with the excluded part removed. For example if you were looking for a random number between 0 and 100 with 70-80 removed, you would find a random number between 0 and 90 (removing the 10 from the excluded range). Then if any value falls above 70 you add the excluded range back. This will preserve the appropriate ratio of randomness for each range and you should see results mostly from the lower range with a few from the upper range because that is a larger percentage of the distribution.

(I've moved the division and rounding out of the function just to make it clearer how it works.)

function xFunction(max, exclude) {  let excluded_range = exclude[1] - exclude[0]  let rand = Math.random() * (max - excluded_range)  if (rand > exclude[0]) rand += excluded_range  return rand}

for (let x = 0; x<10; x++){ let r = xFunction(2250, [1250, 2000]) console.log ((r / 1000).toFixed(3));
}

generate a random number within a range but exclude some in C/C++

A straightforward way is to map the remaining values to an array and use rand() to access them, something like:

int[6] rvec = {1, 3, 4, 6, 8, 10};
// and get numbers with
rvec[rand() % 6];


Related Topics



Leave a reply



Submit