Random number generator in C++

Mani Kireeti
4 min readMay 3, 2020

--

Hi guys. In this article, I’m going to explain how to create random numbers in cpp(c plus plus). Here I’m working in Xcode(11.3). I’ll explain in my future article of how to do cpp coding in mac using Xcode. But now let’s start our article.

The function rand() generates the random numbers we need. For the function to work we should include a certain library named cstdlib. For including this you just need to add the below line to your code at the top.

Now you guys are free to use the rand() function. First I’ll demonstrate how to generate 10 random numbers one after another. The below picture contains the code to do so.

If you observe the code, we are running a loop ten times using the “i” variable. every time I’m declaring a variable named “a” and assigning a variable number to it using the rand() function. The line 17 in the code outputs the value of ‘a’ every time. The output is as below.

Congratulations you have got your ten random numbers generated. Now here’s a catch what happens when we run it again. The output is as below.

Notice anything different ?? The answer is NO. This is because the rand() function generates random numbers based on a given algorithm. And the starting point of the algorithm is always the same. So to get random number’s every time we execute the program we need to use the srand() function. What does the srand() function do?? Well as you thought, it would set the starting point to generate a different set of random numbers every time we execute the program. What happens when we don’t declare srand() is that the compiler assumes srand(1) is called and it produces the same value every time. Any value other than “1” produces different sets of numbers.

As a practice we usually do the following . we use srand(time(0)). This is because the time() function always returns different values, So different sets of numbers are generated.

NOTE: Remember that it is required to use the function srand() only once in the code. I advise you to do it at the starting of the main().

In Xcode, there seems to be some kind of error in using time(0). To clear this error We should use the below instead of srand(time(0)).(ONLY IN XCODE)

srand( static_cast<unsigned int>(time(nullptr)));

So after adding the srand() the output is as followed.

So finally we got different sets of random numbers. YAY!!! Now we will discuss how we will get different sets of random numbers between two integers.

Random numbers between two Given numbers :

Now lets us say we want the generated numbers to be between 1 and 10.How do we do it?? Don’t worry It’s just simple math. We know that rand() function generates a random number of type int. now Let the two numbers between which the random numbers should lie be “max” and “min”. Note that these two numbers are included. Now the below code generates a number between the two numbers max and min.

If we want to create a random number between 10 and 1 (including them) we would do

Selecting between two numbers randomly :

Now we will discuss how to select between two numbers say “a” and “b” randomly using the rand() function. The below code is used to do the work.

You can just type in the above as it is and change the values of a and b as desired.

So This is all about random number generator in cplusplus. Thank You for reading this article.

--

--

Mani Kireeti
0 Followers

computer science engineer @IITMADRAS. One piece fan. Computer geek.