# Setting up a TransformedDistribution with random parameter

I would like to set up a transformed distribution as below:

For each draw from this transformed distribution, I would do the following:

1. Draw x1 from X ~ U(0,1)
2. Draw and output y1 from Y ~ U(0,x1)

I tried setting up transformed distribution using:

TransformedDistribution[x2,
{
x1 \[Distributed] UniformDistribution[{min1, max1}],
x2 \[Distributed] UniformDistribution[{min2, x1  }]
}]

But the above uses a fixed value for the second draw from the uniform distribution.

The corresponding R code for making draws is below where n is the number of draws needed:

draw.rand <- function(n) {
list.rand <- lapply(1:n, function(i) {
vec.max <- runif(1)
vec.rand <- runif(1, min = 0, max = vec.max)
vec.rand})
unlist(list.rand)}

draw.rand(100)

Edit: I would like to evaluate the CDF, PDF of the new distribution.

## Solution

You are after ParameterMixtureDistribution.

ClearAll[dist];
dist[min2_, min1_, max1_] := ParameterMixtureDistribution[
UniformDistribution[{min2, x1}]
, x1 \[Distributed] UniformDistribution[{min1, max1}]
];

PDF[dist[0, 0, 1], x]

CDF[dist[0, 0, 1], x]

Plot[
Evaluate@PDF[dist[0, 0, 1], x]
, {x, 0, 1.2}
, PlotTheme -> "Scientific"
, PlotRange -> {0, 5}
]

## Another example

PDF[dist[1, 2, 3]]

Plot[
Evaluate@PDF[dist[1, 2, 3], x]
, {x, 0, 5}
, PlotTheme -> "Scientific"
]