PHP Random Numbers Quiz

Quiz 6 5 questions

Prerequisites for the quiz

  1. PHP Random Numbers

Are you ready?

5 questions to solve

Instructions
  1. This quiz goes to full-screen once you press the Start button.
  2. At the end of the quiz, you are able to review all the questions that you answered wrong and see their explanations.
Which of the following functions is used to obtain a pseudo-random number in PHP?
The correct function is rand() and this goes with choice (A). Refer to PHP Random Numbers for more information.
Suppose we are using PHP 8.0. Is there any other difference between rand() and mt_rand() besides the different treatment of their $min and $max arguments?

If yes, then what is the difference?

There is absolutely no difference between rand() and mt_rand() apart from the different treatment of their argument. In fact, rand() calls mt_rand() internally. Hence, the correct choice is (A). Refer to PHP Random Numbers for more information.
Which of the following values can be returned by rand(1, 10)?

1, 9 and 10

rand(1, 10) could return any random integer from 1 upto 10, both included. Hence, the correct choice is (D). Refer to PHP Random Numbers for more information.
Which of the given outputs best matches with the following code?
<?php

srand(0);
echo rand(), "\n";

srand(0);
echo rand(), "\n";
Calling srand() twice with the same seed i.e. 0 would result in the subsequent rand() call to return the same number. It's only in choice (A) that both the output numbers are the same and hence it's the correct choice. Refer to PHP Random Numbers for more information.
What does the function srand() do?
The srand() function seeds the PRNG used internally by rand() (and mt_rand()). Hence, the correct choice is (C). Refer to PHP Random Numbers for more information.

"I created Codeguage to save you from falling into the same learning conundrums that I fell into."

— Bilal Adnan, Founder of Codeguage