Logo

JavaScript: Determinism

Functions in any programming language have fundamental properties. They help us understand how a function will behave in different situations, how to test it, and where to use it. One such property 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:

import { length } from 'hexlet-basics/string';

length('hexlet'); // 6
length('hexlet'); // 6

length('wow'); // 3
length('wow'); // 3

No matter how many times we call this function with the argument 'hexlet', it will always return 6.

Non-deterministic functions

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

Math.random(); // 0.09856613113197676
Math.random(); // 0.8839904367241888

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

Deterministic:            Non-deterministic:
length('abc') → always 3  Math.random() → 0.42
length('abc') → always 3  Math.random() → 0.91
length('abc') → always 3  Math.random() → 0.07

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 verify because the result changes.

That is why, where possible, it is better to strive to make a function deterministic.

Instructions

Generate a random integer from 0 to 10 (inclusive) and print it to the screen.

Use Math.random() (returns a number from 0 to 1) and Math.round() (rounds to an integer).

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!