Related Articles

# Output of C programs | Set 33 (rand() and srand())

• Difficulty Level : Easy
• Last Updated : 22 Apr, 2021

You came across how to generate random numbers and use of two C function rand() and srand() from the article rand() and srand() in C\C++.
Listed below are some output related multiple choice question on random numbers.
1. Return type of rand() function is:
a) short
b) int
c) char
d) float

`b`

Explanation : return type of rand() is integer.
2. The function srand(unsigned):
a) Sets the seed for rand()
b) generate random number
c) Is an error
d) generate constant.

`a`

Explanation : srand() always set the seed for rand() function.
3. rand() and srand() functions are used:
a) To find sqrt
b) For and operations
c) For or operations
d) To generate random numbers

`d`

4. The best way to generate numbers between 0 to 99?
a) rand()-100
b) rand()%100
c) rand(100)
d) srand(100)

`b`

Explanation : rand() will generate random number from 0 to RAND_MAX, it’s modulus with 100 ensures that our result must be between 0 and 99 inclusive.
5. Random number between minimum and maximum can be generated by:
a) minimum + (rand() % (maximum – minimum));
b) minimum + (rand() % (maximum – minimum + 1));
c) minimum * (rand() % (maximum – minimum))
d) minimum – (rand() % (maximum+minimum));

`b`

Explanation : (maximum – minimum + 1) is interval size of interval from minimum to maximum, hence rand() % (maximum – minimum + 1) will give us random integer between 0 to interval size which when added to minimum gives random number between minimum and maximum.
6. Which of the following snippet will effectively generate random numbers with widest range?
a) rand();
b) rand(10);
c) rand(time(NULL));
d) All of the mentioned

`a`

Explanation : rand() generate random number between the wide range of 0 to RAND_MAX.
7. For the function call time(), what type of parameter is accepted?
a) int
b) int *
c) time_t
d) time_t *

`d`

Explanation : Parameter for time() is pointer to an object of type time_t, where the time value is stored.
8. What is the output of this C code?

## C

 `#include ``#include ``    ``int` `main()``    ``{``        ``printf``(``"%d\n"``, ``rand``() % 1000);``        ``return` `0;``    ``}`

a) Compile time error
b) An integer between 0-1000
c) An integer between 0-999 including 0 and 999.
d) An integer between 0-1000 including 1000

`c`

Explanation : rand() generate random number and (rand() % 1000) shorten it to range [0, 999].
9. In the below program every time program is run different numbers are generated. True or False?

## C

 `#include ``#include ``    ``int` `main()``    ``{``        ``srand``(``time``(NULL));``        ``printf``(``"%d\n"``, ``rand``());``        ``return` `0;``    ``}`

a) true
b) false
c) Depends on the platform
d) Depends on the compiler

`a`

Explanation : srand() will always set new seed for rand() on each program run.
10. What is the output of this C code?

## C

 `#include ``#include `` ``int` `main()``    ``{``        ``srand``(9000);``        ``printf``(``"%d\n"``, ``rand``());``        ``return` `0;``    ``}`

a) Compile time error
b) An integer in the range 0 to RAND_MAX.
c) A double in the range 0 to 1
d) A float in the range 0 to 1.

`b`

Explanation : rand() will generate integer in range [0, RAND_MAX].
11. What is the output of this C code?

## C

 `#include ``#include ``    ``int` `main()``    ``{``        ``srand``(``time``(NULL));``        ``printf``(``"%d\n"``, ``rand``());``        ``return` `0;``    ``}`

a) Compile time error
b) An integer in the range 0 to RAND_MAX.
c) A double in the range 0 to 1
d) A float in the range 0 to 1.
Explanation : rand() will generate integer in range [0, RAND_MAX], but on running program, every time it will generate different random number because of srand(time(NULL)).
12. What is the output of this C code?

## C

 `#include ``#include ``    ``int` `main()``    ``{``        ``printf``(``"%d\n"``, ``srand``(9000));``        ``return` `0;``    ``}`

a) Compile time error
b) An integer in the range 0 to 9000
c) A float in the range 0 to 1
d) A double in the range 0 to 9000

`a`

Explanation : return type of srand() id object of type time_t.
13. In the below program everytime program is run different numbers are generated.

## C

 `#include ``#include ``    ``int` `main()``    ``{``        ``printf``(``"%d\n"``, ``rand``());``        ``return` `0;``    ``}`

a) true
b) false
c) Depends on the platform
d) Depends on the compiler

`b`

Explanation : for that srand() must be used.
14. Which of these is a correct way to generate numbers between 0 to 1(inclusive) randomly?
a) rand() / RAND_MAX
b) rand() % 2
c) rand(0, 1)
d) None of the mentioned
`a`