250+ TOP MCQs on Pseudorandom Number Generators and Answers

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?
a) int
b) float
c) long
d) double

Answer: a
Clarification: 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()

Answer: a
Clarification: 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

Answer: a
Clarification: 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

Answer: d
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.

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

Answer: d
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.

7. What is the minimum value of RAND_MAX possible in any implementation?
a) 0
b) 32767
c) 2147483647
d) 128

Answer: b
Clarification: 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

Answer: b
Clarification: 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)

Answer: b
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.

10. Pseudo random number generators can be used for data encryption.
a) true
b) false

Answer: b
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.

11. Predict the output of the following code.

#include  
int main() 
{ 
     srand(0); 
     printf("%dn", rand()); 
     return 0; 
}

a) compilation error
b) random number between 0 to RAND_MAX
c) cannot be predicted
d) 0

Answer: b
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.

12. Which header file contains the function rand() in C language?
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.

13. Predict the output of the following code.

#include  
int main() 
{ 
     srand(0); 
     printf("%dn", 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)

Answer: d
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.

14. Which of the following code will generate unique random numbers every time?
a)

#include  
int main() 
{ 
     srand(0); 
     printf("%dn", rand()%50); 
     return 0; 
}

b)

#include  
int main() 
{ 
     srand(time(0)); 
     printf("%dn", rand()%50); 
     return 0; 
}

c)

#include  
int main() 
{ 
    srand(1); 
    printf("%dn", rand()%50); 
    return 0; 
}

d)

#include  
int main() 
{
     printf("%dn", rand()%50); 
     return 0; 
}

Answer: b
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)).

 
 

15. Which of the following code will give an error?
a)

#include  
int main() 
{ 
     printf("%dn", srand()); 
     return 0; 
}

b)

#include  
int main() 
{ 
     srand(time(0)); 
     printf("%dn", rand()%50); 
     return 0; 
}

c)

#include  
int main() 
{ 
     srand(1); 
     return 0; 
}

d)

#include  
int main() 
{
     printf("%dn", rand()%50); 
     return 0; 
}

Answer: a
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().

 
 

Leave a Reply

Your email address will not be published. Required fields are marked *