Data Structures & Algorithms Multiple Choice Questions on “Pseudorandom Number Generators”.
1. What is pseudo random number generator?
a) an algorithm that generates random numbers with help of mathematical formula
b) an algorithm that generates random numbers according to user activity
c) an algorithm that generates random numbers according to time
d) an algorithm that generates random numbers with help of user input
Answer: a
Clarification: A pseudo random number generator generates random numbers with the help of a mathematical formula. We can seed the random number generator with a different value everytime if we want unique random numbers to be generated.
2. What should be the return type of rand() function? Answer: a 3. What is the purpose of using function srand()? Answer: a 4. What is the range of rand()? Answer: a 5.What is the correct formula for generating random numbers in the range (lower,upper) using rand()? Answer: d 6. Which of the following will generate random numbers in the range 1-100 (both inclusive)? Answer: d 7. What is the minimum value of RAND_MAX possible in any implementation? Answer: b 8. Function rand() generates unique random numbers every time. Answer: b 9. What is the default value of seed if function rand() is called before srand()? Answer: b 10. Pseudo random number generators can be used for data encryption. Answer: b 11. Predict the output of the following code. a) compilation error Answer: b 12. Which header file contains the function rand() in C language? 13. Predict the output of the following code. a) compilation error Answer: d 14. Which of the following code will generate unique random numbers every time? b) c) d) Answer: b 15. Which of the following code will give an error? b) c) d) Answer: a
a) int
b) float
c) long
d) double
Clarification: The return type of rand () is int. It can generate random numbers from 0 to RAND_MAX.
a) to set the seed of rand() function
b) to generate random numbers
c) to enable rand() function
d) to improve efficiency of rand()
Clarification: The function srand() sets the seed of rand(). It can be used to generate a unique set of random numbers every time.
a) 0 to RAND_MAX
b) 0 to infinity
c) 0 to 2147483647
d) 0 to 32767
View Answer
Clarification: The function rand() generates random numbers in the range 0 to RAND_MAX. The value of RAND_MAX is minimum 32,767.
a) rand() % (upper – lower)
b) rand() + lower
c) (rand()%(upper-lower)) + lower
d) (rand()%(upper-lower+1)) + lower
Clarification: The correct formula for generating random numbers in the range (lower,upper) using rand() is (rand()%(upper-lower+1)) + lower. The function rand() generates random numbers in the range 0 to RAND_MAX.
a) rand() % 100
b) rand() % 101
c) (rand() % (101)) + 1
d) (rand() % (100)) + 1
Clarification: Formula for generating random numbers in the range (lower,upper) using rand() is (rand()%(upper-lower+1)) + lower. So the correct answer will be (rand() % (100)) + 1.
a) 0
b) 32767
c) 2147483647
d) 128
Clarification: The value of RAND_MAX varies according to implementation. But it is guaranteed to be at least 32767.
a) true
b) false
Clarification: Function rand() does not generate unique random numbers every time. For achieving this we have to use srand() along with rand().
a) srand(0)
b) srand(1)
c) srand(time(null))
d) srand(-1)
Clarification: If srand() is not called before the call to the function rand() then the value of seed is taken as srand(1) by default. We should use srand() before rand() in order to use our own seed.
a) true
b) false
Clarification: Pseudo random number generators cannot be used for data encryption as the random numbers generated by them are predictable. For data encryption the numbers should be absolutely unpredictable.#include
b) random number between 0 to RAND_MAX
c) cannot be predicted
d) 0
Clarification: The function rand() generates random numbers in the range 0 to RAND_MAX. The function srand() seeds rand(). We get unique set of random numbers if rand() is seeded with a different value every time.
a) stdlib
b) iostream
c) stdio
d) time
Answer: a
Clarification: In C language the header file stdlib contains the function rand(). It generates pseudo random numbers.#include
b) random number between 0 to 50 (both inclusive)
c) random number between 0 to 51 (both inclusive)
d) random number between 0 to 49 (both inclusive)
Clarification: Formula for generating random numbers in the range (lower,upper) using rand() is (rand()%(upper-lower+1)) + lower. So the given code will generate random numbers between 0 to 49.
a)#include
#include
#include
#include
Clarification: The function rand() generates random numbers in the range 0 to RAND_MAX. The function srand() seeds rand(). We get a unique set of random numbers if rand() is seeded with a different value every time. This can be achieved by using srand(time(0)).
a)#include
#include
#include
#include
Clarification: The function rand() generates random numbers in the range 0 to RAND_MAX. The function srand() seeds rand(). So we get an error if we try to print srand().