There is a range of numbers, say "1 .. n", and there is a second range of numbers, say "2n .. 3n". I want to implement a sample of one random value from these two ranges of numbers.
Some kind of something like:

r:=RandomRange(RandomRange(1, 10), RandomRange(1000, 2000)); 

But after all, so here RandomRange will not cope with the task.
Those. In this example, in the generated random numbers, numbers in the range from 11 to 999 will be excluded.

    2 answers 2

    So:

     r:=RandomRange(1,1010); if r>=10 then r:=r+990; 

    upd.

    1. The total numbers in both ranges are 1009. (from 1 to 9, and from 1000 to 1999).
    2. We take random number from 1 to 1010 - just 1009 numbers.
    3. If you are in the first range, then everything is OK, if you are out of the first range, then you are in the second.
    4. Similarly, you can choose from three or more with an array and a cycle. For two enough two lines, see above.
    • Thank! Indeed, ingenious and simple. - I_CaR

    To obtain equally probable results from 2 or more ranges, do this:

    1. Count the "width" of each range ( 9, 1000 )
    2. Put the ranges together one by one - for each range, save its beginning, end and original beginning (you will get an array of the type [0,9,1], [10,1009,1000] )
    3. Select a random number from 0 to the maximum (end of last range + 1) ( Random(1010) )
    4. Determine which range the number fell into (in a loop, go through the array and check if the number is included in the boundaries) (for example, 768 falls into the 2nd range)
    5. Subtract the beginning of the range from the number and add the beginning of the original range ( 768 - 10 + 1000 )
    6. Is done