Generate random numbers in C++
In this post, you will learn how to generate the random numbers in C++.
Such a type of logical question is generally asked in an interview or in competitive exams. This can be helpful in improving your logical programming skills and is helpful in game development and other logical application development. For instance, in a round of dice, without having arbitrary occasions, we will have similar side springing up each time we toss the dice in this way giving bothersome outcomes. Consequently it becomes important that we have a random number generator available to us.
There are different ways to generate random numbers in C++. Here, we have mentioned most of them.
C++ generate random number using srand()
The srand() function in C++ seeds the pseudo-random number generator used by the rand() function. It is defined in the cstdlib header file.
The syntax of srand()srand(unsigned int seed);
This function takes a seed value as a parameter of type unsigned int. It does not return any value. It sets the seed for the rand() function. The seed for rand() function is 1 by default.
It actually intends that if no srand() is called before rand(), the rand() work acts as if it was seeded with srand(1).
Example 1
In the given example, we have taken different seed values for generating the random numbers.
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
printf("Randomly generated numbers: ");
int i = rand();
printf(" %d ", i);
srand(2);
int j = rand();
printf(" %d ", j);
srand(time(0));
int k = rand();
printf(" %d ", k);
}
Output of the above code:
Randomly generated numbers: 1804289383 1505335290 439230447
Example 2
Here, we have created different sequence of random numbers using for loop.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void){
// Use current time as seed for random generator
srand(time(0));
printf("Randomly generated numbers: ");
//Random numbers generated between 1 and 6
for(int i = 0; i<6; i++)
printf(" %d ", rand());
return 0;
}
Output of the above code:
Randomly generated numbers: 2071673691 1363650630 1641768912 1186119441 1722658809 59170489
Example3
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
printf("First number: %d\n", rand()%100);
srand(5);
printf("Random number: %d\n", rand()%100);
srand(1);
printf("Again the first number: %d\n", rand()%100);
return 0;
Output of the above code:
First number: 83
Random number: 75
Again the first number: 83
C++ generate random number using rand()
The rand() function is used to generate random numbers in the range(0, RAND_MAX). The maximum value of the rand() is RAND_MAX.
The given program generate random numbers between 1 and 10 using the rand() function.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
// Initialize random number generator
srand(time(0));
cout<<"Random numbers generated between 1 and 10:"<<endl;
for(int i=0;i<10;i++)
cout << (rand() % 10) + 1<<" ";
return 0;
}
Output of the above code:
Random numbers generated between 1 and 10:
1 7 3 2 10 8 3 1 9 4
Related Articles
Implementation of queue using array in CQueue implementation in Python
Queue Implementation in C
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Prime factors of a number in c
Armstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to convert Decimal to Octal
C program to convert decimal to binary
C program to check whether a number is even or odd