Range Excluding Random Numbers

How do you produce a random number within a range, excluding certain ranges. For example. range 1-10, but not 2-4 or 7. The solutions I have used so far are:

  • Create a random test if it is within the allowed range. Based on the result, number or try again.
  • The card allowed varies to a uniform range. Get random between 1 and 6, and then display backward (i.e. 6 becomes 10).
  • Create valid ranges (1-1.5-6.8-10). Randomly select a range (optional use scales) and a number in the selected range.

What is your decision?

+6
language-agnostic random
source share
5 answers

(b) Use the same range and map for valid values.

(a) It is slower and the runtime is not deterministic, because you need to wait until you get a number in the correct range. If you missed a large range, you would be free.

(c) more complex than (b); do not add complexity if it is not required.

+8
source share

This will depend on how many / large exception ranges. Testing the allowed range (your option 1) will work fine for small sets; no need to complicate the solution for an easy problem. Solution 3 will work better for more numerous sets of exceptions. Solution 2 is the most effective, but probably the most correct theoretical solution.

+1
source share

I usually use the method described above by the bullet number, especially if the set of valid numbers is small enough. From a statistical point of view, this is a way to easily either destroy the randomness of the results or distort the results from a flat distribution.

This has the added benefit of allowing you to make one choice (for example, hand cards or collecting bingo balls) ... you simply delete the values ​​you have already selected from the card.

+1
source share

Compare them to the total sum of the expected ranges. then distribute them between the ranges.

eg. if you need a random value between 0..10 and 100..110

Create a random number between 20. The bottom 10s are in the range 0..10, the rest are in a different interval (or something like that - I can disconnect by one). Interval arithmetic is one of these things that I can never handle on the first try).

The reason for this is because you often deal with not perfect random generators. They start to behave strangely if you distribute consecutive random number variables in several dimensions (for example, first select a random interval, then select a random interval in the selected interval). This can lead to very obvious nonrandom behavior.

If you start with a better random number generator that receives data from true random sources, you can end up wasting precious random bits. If you do this only once per second, this may not be a problem. If you do this often, although the program may stall because pure random sources must catch up with your random bit.

+1
source share

It looks like your algorithm can benefit from a small redesign that will make random numbers implicit, rather than explicitly find them using a random number generator.

For example, if you want to get a random series of numbers from 1 to 10, it is better to start with an ordered series, mix it in some way, for example, by exchange (there was a question about this, I think) and repeat the numbers one by one.

0
source share

All Articles