# Simulate random sample from defined probability density function

Suppose I define a function in mathematica that has all of the properties of a valid probability density function. Would it be possible to have Mathematica simulate a random sample of a given size of values from this PDF? For example, if I define a function which is the PDF of a beta distribution, can I collect ten thousand "instances" or random samples from this PDF for statistical analysis? If so, how would I do this? Despite my example, I am specifically interested in being able to use my own defined PDF, not necessarily a built-in one that Mathematica already has.

• seen Oleksandr Pavlyk's Creating Your Own Distribution in Wolfram Conference 2011?
– kglr
Feb 14, 2018 at 2:03

You can use ProbabilityDistribution for this. Your example pdf:

pdf = PDF[BetaDistribution[1.2, 3.2], x];
pdf //TeXForm


$\begin{cases} 4.554304156477517 (1-x)^{2.2} x^{0.19999999999999996} & 0<x<1 \\ 0 & \operatorname{True} \end{cases}$

And using ProbabilityDistribution:

RandomVariate[ProbabilityDistribution[pdf, {x, 0, 1}], 10]


{0.0287507, 0.274103, 0.1581, 0.524414, 0.496944, 0.0579204, 0.204799, 0.230026, 0.140485, 0.438046}

• This is helpful, thank you. But suppose I want to use my own PDF, (say one that I have defined as a function), not one that Mathematica has built-in. How would I do that? Feb 14, 2018 at 2:02
• @ereHsaWyhsipS Just replace pdf` with your chosen function. Feb 14, 2018 at 2:04