2
$\begingroup$

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.

$\endgroup$

3 Answers 3

12
$\begingroup$
RandomReal[1, {100, 2}] /. {x_, y_} /; y > x :> {y, x}

The graphics below confirm that you retain a uniform distribution

Mathematica graphics

$\endgroup$
5
$\begingroup$

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}}
$\endgroup$
2
  • 3
    $\begingroup$ 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}. $\endgroup$
    – Mr.Wizard
    Jul 25, 2014 at 20:41
  • 1
    $\begingroup$ Note: While the second method has problems the first is correct, and it is four times faster than RunnyKine's (admittedly pretty) method. $\endgroup$
    – Mr.Wizard
    Jul 26, 2014 at 9:52
4
$\begingroup$

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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