How do you generate a random number within a range while excluding certain range(s). Eg. range 1-10 but not in 2-4 or 7. Solutions I've used so far:
What is your solution?
(b) Use the single range and map to allowed values.
(a) Is slower and running time is non-deterministic because you have to wait until you get a number in the right range. If you were to skip a large range, you'd be hosed.
(c) Is more complex than (b); don't add complexity if it isn't required.
It would depend on how many/big the exclusion ranges are. Testing for dis-allowed range (your option 1) would work fine for small sets; no need to complicate the solution to an easy problem. Solution 3 would work better for more numerous exclusion sets. Solution 2 is the most work, but probably the most correct theoretical solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With