Randomly generated numbers with inequality constraints I have to generate two non-negative random numbers (uniform distribution) $a'$ and $b'$ subject to the following conditions (the min/max_x variables are non-negative and user defined):

*

*$a\in [\min_a, \max_a]$

*$b'\in [\min_b, \max_b]$

*$a' \leq b'$
If the ranges $[\min_a, \max_a]$ and $[\min_b, \max_b]$ makes it possible to generate a pair such that $a' > b'$, and the pair you generated did indeed violate the condition, then you are allowed to do whatever you want to solve the situation as far as:

*

*If there's a solution $(a', b')$ inside the definition interval of both random variables, your solution must be inside the definition intervals.

*If there's not (because $\max_b < \min_a$), then you are allowed to choose an $a' < \min_a$.

Some ideas:

*

*Swap $a'$ and $b'$, or maybe assign $a'\leftarrow b'$, and if $a'>\max_a$, then assign $a' \leftarrow \max_a$.

*Do the least aggresive modification to the definition intervals so that $\max_a \leq \min_b$ to make invalid solutions impossible.

*If the definition interval contains a solution, generate new pairs again until a solution is found (very inneficient computationally if the definition intervals makes valid solutions more unlikely than invalid ones; I prefer not to choose that path).

*Keep $a'$ fixed and choose a new random $a'\in [0,b']$.

*(I like this one): choose $b'$, and then choose a random $a'\in[\min(\min_a, b), \min(\max_a, b)]$. The problem with this approach is that, if the generated $b'$ is too lower, it could generate an invalid solution while valid solutions are still possible (?).

What method do you recommend me so that the it's the most faithful as possible respect to original distribution? Because I don't want to choose a method that makes some cases much more likely than others or that it moves the definition intervals too far away from the original ones.
 A: So assuming your intervals overlap. If $\min_b\leq \min_a$ you can simply choose two numbers from $[\min_a,\max_b]$ and assign the smaller one to $a$ and the larger one to b. That way you should obtain a uniform distribution over the allowed cases.
If however $\min_b >\min_a$ it gets a bit more difficult. You have to choose a $b$ first with a non-uniform probability, s.t. $Prob(b) \propto \min\{b-\min_b,\max_a-\min_b\}$ and then you choose an $a$ uniformly from $[\min_a, b]$.
If you don't need an exact answer in the second case you can stick with the following: if $\min_b$ is not much larger than $\min_a$ (and thus the allowed region is "almost a triangle") you can simply choose a coordinate uniformly from the rectangle and if it is in the forbidden region mirror it by the diagonal into the allowed region. That will get you an almost uniform distribution. And if $\min_b$ is much larger than $\min_a$ (and thus the allowed region is "almost a rectangle") you can simply choose $b$ uniformly and than $a$ from $[\min_a,b]$. Again that will then be an almost uniform distribution.
Edit: you can visualize it on a coordinate system where one axis is $a$ and one is $b$. The intervals $[\min_a,\max_a]$ and $[\min_b,\max_b]$ then produce a rectangle. If you then draw the diagonal on which $a=b$ you can see where you have to cut your rectangle. That is the allowed region.
