C++

C++ Random Number Between x and y ( Generate a Number from Any Range)2 min read

In this tutorial we will get a random number between any range using C++ Program.

Before diving the example you shuld know what are srand and rand functions in C++.




srand : This function sets the starting point for producing a series of pseudo-random integers. If srand() is not called, the rand() seed is set as if srand(1) were called at program start. Any other value for seed sets the generator to a different starting point.

rand: This number is generated by an algorithm that returns a sequence of apparently non-related numbers each time it is called. This algorithm uses a seed to generate the series, which should be initialized to some distinctive value using function srand.

C++ Random Number Between 0 and 1:

Output:

C++ Random Number Between 1 and 9:

Output:

C++ random number between 1 and 1000:

Output:

C++ random number between 1 and 6:

Output:

C++ random number between 10 and 50:

Output:

Leave a Comment