This lesson will look at some practical applications of random number generation.

Applications: Generating random numbers

Now we can do wonders with this random number generation. Following are two instructions we should keep in mind:

srand(time(0));  // preferably the first line in the main
int x = rand();

Let’s look at some applications of random number generation.

Tossing a coin

When we toss a coin, it gives either a head or a tail. We can mimic this by generating a random 0 or 1. How can we accomplish that?

Generate a random number by calling the rand() function and take the modulus by 2. It will map every value to either 0 or 1.

Let’s write a one-line expression for generating a coin toss:

Get hands-on with 1200+ tech skills courses.