Here is a listing of C++ questions on “Random Numbers” along with answers, explanations and/or solutions:
1. Which header file is used to create the pseudo random generator?
a) random
b) cstdlib
c) rand
d) both random and cstdlib
View Answer
Explanation: cstdlib header file is used to create pseudo random number. C++11 standard added random header file as well to generate random numbers.
2. Which is a constant defined in <cstdlib> header file?
a) RAND_MAX
b) Rand
c) Srand
d) Blitz
View Answer
Explanation: RAND_MAX is a constant defined in <cstdlib> for deciding the maximum random number that can be produced.
3. How many parameters are available in srand function?
a) 1
b) 2
c) 3
d) 4
View Answer
Explanation: There is one parameter available in srand function. That is an integer value to be used as seed by the pseudo-random number generator algorithm.
4. What will be the output of the following C++ code?
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int ran = rand();
cout << ran << endl;
}
a) Any number
b) 89
c) 0​ to RAND_MAX
d) 79
View Answer
Explanation: As the declared number is integer, It will produce the random number from 0​ to RAND_MAX. The value of RAND_MAX is library-dependent, but is guaranteed to be at least 32767 on any standard library implementation.
Output:
$ g++ rand.cpp $ a.out 574
5. What will be the output of the following C++ code?
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
cout << RAND_MAX << endl;
}
a) 4385234
b) 12321412
c) Depends on the compiler
d) 32564789
View Answer
Explanation: RAND_MAX is a function used by the compiler to create a maximum random number.
Output:
$ g++ rand1.cpp $ a.out 2147483647
6. What will be the output of the following C++ code?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
srand (time(NULL));
printf ("Random number: %d\n", rand() % 100);
srand (1);
return 0;
}
a) 12
b) 23
c) 33
d) Any number from 0 to 99
View Answer
Explanation: This program will create a random number based on time function using srand function.
Output:
$ g++ rand2.cpp $ a.out 23
7. What will be the output of the following C++ code?
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
srand((unsigned)time(0));
int ran;
for (int i = 0; i < 2; i++)
{
ran = (rand() % 10) + 1;
cout << ran;
}
}
a) 2 4
b) 10 20
c) Any two number from 1 to 10
d) 50 6
View Answer
Explanation: In this program, It will produce two numbers from 1 to 10 by using srand function.
Output:
$ g++ rand3.cpp $ a.out 4 5
8. What will be the output of the following C++ code?
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand((unsigned)time(0));
int ran;
int low = 1, high = 10;
int range = (high - low) + 1;
for(int index = 0; index < 1; index++)
{
ran = low + int(range * rand() / (RAND_MAX + 1.0));
cout << ran << endl;
}
}
a) 1
b) 2
c) 3
d) 4
View Answer
Explanation: In this program the output will always be 1 because as one can observe from the formula used in line #13 that the term inside int() will always be zero. Hence the output will always be 1.
Output:
$ g++ rand4.cpp $ a.out 1
9. Which operator is used to produce a certain number in a specific range?
a) $
b) %
c) modulo operator
d) both % and modulo operator
View Answer
Explanation: In C++, modulo operator is denoted by %.
10. Which can be used to create a random number without duplication?
a) Character
b) Time
c) Both Character & Time
d) Date
View Answer
Explanation: Time can be used to create a random number without duplication.
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check C++ Books
- Practice Programming MCQs
- Apply for C++ Internship
- Check Computer Science Books
- Practice Computer Science MCQs