Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

How can I create a list of random pairs $(x,y)$ where $0<y<x<1$ and $x,y \in \mathbb{R}$ ? I can't seem to place that inequality restriction on the random numbers that are generated.

share|improve this question

3 Answers 3

up vote 12 down vote accepted
RandomReal[1, {100, 2}] /. {x_, y_} /; y > x :> {y, x}

The graphics below confirm that you retain a uniform distribution

Mathematica graphics

share|improve this answer

Generate random pairs, and reverse-sort each one:

Sort[#, Greater] & /@ RandomReal[1, {10, 2}]

or, for fun, generate two numbers between 0 and 0.5, and add the second to the first:

RandomReal[0.5, {10, 2}].{{1, 0}, {1, 1}}
share|improve this answer
3  
I don't believe the second method is valid: the smaller value can never exceed 0.5, incorrectly ruling out e.g. {0.8, 0.7}. –  Mr.Wizard Jul 25 at 20:41
1  
Note: While the second method has problems the first is correct, and it is four times faster than RunnyKine's (admittedly pretty) method. –  Mr.Wizard Jul 26 at 9:52

Another way is

{#, RandomReal[#]} & /@   RandomVariate[TriangularDistribution[{0, 1}, 1], 1000]

To see we do have a uniform distribution:

PDF[
    TransformedDistribution[{x, y}, {
                x  TriangularDistribution[{0, 1}, 1],
                y  UniformDistribution[{0, b}]
                }] /. b -> x,
    {x, y}]

uniform distribution on 0<y<x<1

share|improve this answer
2  
It looks like that is NOT an uniform distribution. –  Silvia Jul 25 at 20:12
    
@Silvia yes it is not uniformly distributed –  Algohi Jul 26 at 3:51
2  
Maybe something like {#, RandomReal[#]} & /@ RandomVariate[TriangularDistribution[{0, 1}, 1], 100000]? –  Silvia Jul 26 at 8:08
    
@Silvia Very good. Either edit this Answer to include that or post your own. –  Mr.Wizard Jul 26 at 9:51
    
@Mr.Wizard done :) –  Silvia Jul 26 at 11:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.