This set of Data Structures & Algorithms Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer
Explanation: 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?
a) int
b) float
c) long
d) double
View Answer
Explanation: The return type of rand () is int. It can generate random numbers from 0 to RAND_MAX.
3. What is the purpose of using function srand()?
a) to set the seed of rand() function
b) to generate random numbers
c) to enable rand() function
d) to improve efficiency of rand()
View Answer
Explanation: The function srand() sets the seed of rand(). It can be used to generate a unique set of random numbers every time.
4. What is the range of rand()?
a) 0 to RAND_MAX
b) 0 to infinity
c) 0 to 2147483647
d) 0 to 32767
View Answer
Explanation: The function rand() generates random numbers in the range 0 to RAND_MAX. The value of RAND_MAX is minimum 32,767.
5.What is the correct formula for generating random numbers in the range (lower,upper) using rand()?
a) rand() % (upper – lower)
b) rand() + lower
c) (rand()%(upper-lower)) + lower
d) (rand()%(upper-lower+1)) + lower
View Answer
Explanation: 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.
6. Which of the following will generate random numbers in the range 1-100 (both inclusive)?
a) rand() % 100
b) rand() % 101
c) (rand() % (101)) + 1
d) (rand() % (100)) + 1
View Answer
Explanation: 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.
7. What is the minimum value of RAND_MAX possible in any implementation?
a) 0
b) 32767
c) 2147483647
d) 128
View Answer
Explanation: The value of RAND_MAX varies according to implementation. But it is guaranteed to be at least 32767.
8. Function rand() generates unique random numbers every time.
a) true
b) false
View Answer
Explanation: Function rand() does not generate unique random numbers every time. For achieving this we have to use srand() along with rand().
9. What is the default value of seed if function rand() is called before srand()?
a) srand(0)
b) srand(1)
c) srand(time(null))
d) srand(-1)
View Answer
Explanation: 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.
10. Pseudo random number generators can be used for data encryption.
a) true
b) false
View Answer
Explanation: 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.
11. Predict the output of the following code.
#include <stdlib.h> int main() { srand(0); printf("%d\n", rand()); return 0; }
a) compilation error
b) random number between 0 to RAND_MAX
c) cannot be predicted
d) 0
View Answer
Explanation: 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.
12. Which header file contains the function rand() in C language?
a) stdlib
b) iostream
c) stdio
d) time
View Answer
Explanation: In C language the header file stdlib contains the function rand(). It generates pseudo random numbers.
13. Predict the output of the following code.
#include <stdlib.h> int main() { srand(0); printf("%d\n", rand()%50); return 0; }
a) compilation error
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)
View Answer
Explanation: 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.
14. Which of the following code will generate unique random numbers every time?
a)
#include <stdlib.h> int main() { srand(0); printf("%d\n", rand()%50); return 0; }
b)
#include <stdlib.h> int main() { srand(time(0)); printf("%d\n", rand()%50); return 0; }
c)
#include <stdlib.h> int main() { srand(1); printf("%d\n", rand()%50); return 0; }
d)
#include <stdlib.h> int main() { printf("%d\n", rand()%50); return 0; }
Explanation: 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)).
15. Which of the following code will give an error?
a)
#include <stdlib.h> int main() { printf("%d\n", srand()); return 0; }
b)
#include <stdlib.h> int main() { srand(time(0)); printf("%d\n", rand()%50); return 0; }
c)
#include <stdlib.h> int main() { srand(1); return 0; }
d)
#include <stdlib.h> int main() { printf("%d\n", rand()%50); return 0; }
Explanation: 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().
Sanfoundry Global Education & Learning Series – Data Structures & Algorithms.
To practice all areas of Data Structures & Algorithms, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check Computer Science Books
- Check Design and Analysis of Algorithms Books
- Practice Computer Science MCQs
- Practice Data Structure MCQ
- Practice Programming MCQs