Random function in C++ with range

In this article, we’ll talk about a C++ function that is commonly used for gaming and security purposes in order to generate a random number from a specified range.

C++ includes a built-in pseudo-random number generator with two functions for generating random numbers:

• rand()
• srand()

So we’ll use these two functions, along with several examples, in this discussion.

rand() in C++ :

We must first include the “cstdlib” header file before we can use the rand() function. Every time the program runs, this rand() function will generate a random number in the range[0, RAND_MAX). Where RAND_MAX is a constant that is at least 32767. The numbers that are generated each time are unrelated and random. In a scenario where we do not call srand() before using rand(), the program will generate the same sequence of numbers every time it runs.

Syntax :

int rand() //with no parameters

Below is the code where I have used the rand() function and assign it to a variable named “random_number“.

#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
int random_number = rand();
cout<<random_number<<"\n";
return 0;
}

Output :

1804289383

I got the same number as the output every time I run the above program. However, our true goal is to generate a random number each time we execute the program. So, in order to accomplish this, we will use another function srand().

srand() along with rand() :

Similar to the rand() function, srand() is also present in the “cstdlib” header file in CPP and is used to initialize the random number generators. We pass the seed parameter (where the seed is for a new sequence of pseudo-random numbers to be returned by successive calls to the rand function).

Syntax :

void srand(unsigned int seed);

The majority of the time, we use system time as the seed value to generate unique output on each run. Because system time will vary from time to time. As a result, the pseudo-random number changes each time we run the program. Below is a code to understand it better.

#include <iostream>
#include <cstdlib>
using namespace std;

int main() {
srand((unsigned) time(0));
int random_number = rand();
court<<random_number<<"\n";
return 0;
}

Output:

386457086

Output:

1039511611

And so on, Because the output keeps on changing each time we are running the above program. The output may be varied for you while you’re running the same code, it’s because of a change in system date each time we call the function.

Generating the random numbers within a range in C++ :

Using the modulus operator, you can produce random integers within any range. To generate integers between 1 and 100, for example, use int random_number = 1+ (rand()% 100).

Syntax :

int var_name = startingPoint + (rand() % range)

Where the range is the number of values between the start and the end of the range, inclusive of both.

Let’s see some examples to understand it better,

Suppose we want to display random numbers from 1 to 6 inclusive both each time we roll dice in a gaming application. We can use this rand() function as follows :

#include <iostream>
#include <cstdlib>
using namespace std;

int main() {
srand((unsigned) time(0));
int random_number = 1 + (rand() % 6);
cout<<"The dice displays value ";
cout<<random_number<<"\n";
return 0;
}

Output :

The dice displays value 2

Output :

The dice displays value 4

and so on., the output keeps on changing each time we run the program.

Let’s see some more examples over here :

• If, var = 2002 + (rand() % 21) then, var is going to display values in the range from 2002-2022 both inclusive.
• Similarly, if var = rand() % 100 then, var is going to display values in the range from 0 to 99 both inclusive.

Hope you have understood the whole discussion.