Random numbers are important in many real life scenarios such as computer games and statistical solutions and some computer algorithms. Nature is also full of randomness so to simulate natural objects such as clouds, mountains and tree we need random numbers.

Table of Contents

C Standard Library provides two different methods to generate random numbers. They are:

Use rand() and srand() methods

The rand() function alone generates the same sequence of numbers in every program run. This function can generate a random number between 0 and RAND_MAX. Here RAND_MAX macro value is at least 32767 which is the minimum number in any standard library implementation i.e. rand() function should be able to generate any random number between 0 and 32767.

Here is a simple C program to generate 10 random numbers.

The drawback of the above program is that it prints the same random numbers every time it runs. In order to overcome the issue, another method is used to initialize the random number generator i.e. srand(). This function uses the argument (unsigned int) passed as seed. Every time a different seed is passed to this function, call to rand() function will generate different random number.

In order to provide a different seed, time() function can be used to provide a different seed every time program runs i.e. time(0).

Random Number Generator Program

Now with the help of these two functions we can easily create a random number generator program that can generate random numbers between a range of numbers. We will use modulus operator to generate the numbers between a range.

Tips and Tricks

Here are some additional tips and tricks for working with random numbers in C/C++.

  1. Seed the Random Number Generator: While the srand() function is used to seed the random number generator, it’s important that the seed value should be different for every run. One common approach is to use the current time as the seed value as we show in previous example.
  2. Generate Random Numbers Within a Range: The rand() function generates a random number between 0 and RAND_MAX. To generate a random number within a specific range, such as between 1 and 100, you can use the modulus operator (%). For example, to generate a random number between 1 and 100, you could use rand() % 100 + 1.
  3. Avoid Common Pitfalls: There are some common pitfalls to watch out for when generating random numbers, such as forgetting to seed the random number generator or generating the same random number multiple times.
  4. Use Pseudo-Random Number Generators (PRNGs) for Certain Applications: PRNGs are algorithms that generate a sequence of random numbers that appear to be random but are actually determined by a specific formula. While they’re not truly random, they can be useful for certain applications, such as simulations or games.
  5. Use Hardware Random Number Generators for More Secure Applications: For applications that require truly random numbers, such as cryptography, hardware random number generators (HRNGs) are often used. These devices generate random numbers based on physical processes, such as thermal noise or radioactive decay.