Logo
/
Programming
/
PHP Course
/

Determinism

PHP: Determinism

Functions in any programming language have fundamental properties. These properties help you understand how a function will behave in different situations, how to test it, and where to use it. One of these properties is determinism.

A deterministic function always returns the same result for the same input data. For example, a function that counts the number of characters can be called deterministic:

<?php

strlen('hexlet'); // 6
strlen('hexlet'); // 6

strlen('wow'); // 3
strlen('wow'); // 3

The strlen() function can be called endlessly with the same argument, and it will always return the same result.

Non-deterministic functions

The opposite type is non-deterministic functions. They can return different results for the same input data or in the absence of it (functions without parameters). A good example is the rand() function, which returns a random number:

<?php

rand(); // 827606195
rand(); // 635369726

This function has no arguments, but its result is different every time. If even one call out of millions gives a different result, the function is considered non-deterministic.

Deterministic:              Non-deterministic:
strlen('abc') → always 3    rand() → 827606195
strlen('abc') → always 3    rand() → 635369726
strlen('abc') → always 3    rand() → 142503087

Why this matters

Determinism affects how we work with functions.

  • deterministic functions are easy to test and predict;
  • they are easier to optimize and reuse;
  • non-deterministic functions are harder to check, because the result changes.

That's why, wherever possible, it's better to aim for a function to be deterministic.

Instructions

In a board game, you roll a die and get a number from 0 to 10. Write a program that simulates this roll: it prints a random integer in the range from 0 to 10 to the screen. For this task you'll need the rand() function, which can take two numbers: the start and the end value of the range.

Try to solve this task in a single line.

Tips

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Found a bug? Have something to add? Pull requests are welcome!