## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

# Rejection sampling

So far, we have showed you how to simulate data from well-defined distributions. However, you may also wish to simulate data from an unknown distribution. The first method we will show you to simulate random variables is through rejection sampling. This method is based on the idea that if you want to sample a random variable from a target distribution, you just have to sample the region under the curve of its density function. For example, let's consider a triangle distribution defined by the following density function:

First, let's plot the function in R by creating a `triangle()` function, as follows:

`> triangle <- function(x) {(abs(x) ...`

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required