Get startedGet started for free

Random number generation

1. Random number generation

In this last chapter, you will pratice your new skills on a selection of mini case studies.

2. Generating single random numbers

One of the strengths of R is its math library, and particularly its random number generation capabilities. You normally use these capabilities from the R functions in the "stats" package, such as "rnorm()", "runif()", and so on. The internal R API makes these available too, but as functions that generate single random numbers, not vectors. That is of course not a problem as we can always write a loop if we want many. Rcpp makes the function from the R API available under the R namespace, and because you typically don't specify the use of this namespace the same way you do with the Rcpp namespace, you have to prefix calls to these functions. So to generate a number from a normal distribution with mean 0 and standard deviation 1, you would use R colon colon rnorm(0, 1).

3. Generating vectors

The additional reason for using the R namespace prefix is that Rcpp also defines function with the same names to generate vectors of random values. These functions belong to the Rcpp namespace, so you typically don't need to prefix them because you probably have a use namespace Rcpp directive somewhere at the top of your C++ file. These functions behave similarly to their equivalent functions in the stats package, so they can be useful. However, you could easily reproduce their behaviour with the ones from the R namespace by using a simple for loop.

4. Generating numbers from a truncated distribution

Now that we know how to generate numbers from the classic distributions, we can create samplers from more exotic distributions. Let's first have a look at generating numbers from a truncated distribution. For example, let's say you want to generate numbers from a normal distribution with mean 2 and standard deviation 2, but truncated at 0 so that you only sample positive values.

5. Rejection sampling

There are many ways to do this, but let's use rejection sampling. The idea is pretty simple and brute force. Just keep generating a number until it is in the right interval. You will have to use two nested loops, the outer loop goes through the vector to fill in the numbers, in this case, a for loop makes perfect sense. For the inner loop, that you use to generate each number, you don't know how many times it needs to run, but it needs to run at least once. In this case a "do-while" loop is ideal.

6. Generate from a mixture of distributions

The other example in the random number generation case study is about generating numbers from a mixture of distributions. Once again, you only need to worry about generating one of these, and you will use a for loop to generate a vector. The mixture is made of p components, and each of these components has a weight.

7. Generate from a mixture of distributions

To generate a number from a mixture, you first have to decide which component of the mixture it belongs. This is typically done by generating a number from a uniform distribution between 0 and the sum of the component weights. Then scanning the vector of weights to decide. Choosing the component is clearly a separate operation, so you will do this in a function, but you don't particularly need that function to be exposed to the user, so you won't export it. Aftwerwards, given that component function, you just need to generate a number from the selected component using its parameters.

8. Let's practice!

Onto generating random numbers!