DEV Community

Cover image for Is random numbers in computers are random at all? JS version
Aditya
Aditya

Posted on

Is random numbers in computers are random at all? JS version

Introduction

Randomness in programming often appears mysterious, especially when you consider that computers operate deterministically, relying solely on 0s and 1s. This article dives deep into how randomness is simulated in JavaScript using the Math.random() function and how computers manage to generate what we perceive as "random numbers."

Random-numbers

How Computers Generate "Randomness"

Computers are deterministic by nature—they follow a strict sequence of operations. So, how do they produce "random" numbers?

Random

Pseudo-Random Number Generators (PRNGs)
The randomness generated by Math.random() is not truly random but pseudo-random. A PRNG uses a mathematical formula or an algorithm to produce a sequence of numbers that appears random.

Key aspects of a PRNG:

  1. Seed Value: The generator starts with an initial value, called a seed. This seed determines the sequence of numbers produced.
  2. Deterministic Nature: If you know the seed and the algorithm, you can predict the sequence of numbers.
  3. Periodicity: PRNGs eventually repeat their sequences after a certain number of iterations.

In JavaScript, Math.random() is typically implemented using an algorithm like the XorShift or Mersenne Twister (the exact implementation depends on the JavaScript engine, such as V8 in Chrome).

random-number

How Does Math.random() Work?

In JavaScript, Math.random() is the most common method used to generate a random number. Here's what it does:

It returns a floating-point number between 0 (inclusive) and 1 (exclusive).
For example, calling Math.random() might return numbers like 0.2315601941492, 0.6874206142281, or 0.9912760919023.

// Generate a random number between 0 and 1
console.log(Math.random());

// Generate a random integer between 0 and 9
console.log(Math.floor(Math.random() * 10));

// Generate a random number between 1 and 100
console.log(Math.floor(Math.random() * 100) + 1);
Enter fullscreen mode Exit fullscreen mode

Breaking Down Math.random()

Let’s understand the underlying steps:

  1. A seed value is used to initialize the generator. In most JavaScript engines, this seed might be derived from the system clock or some other unique value.
  2. The algorithm applies a series of mathematical transformations to the seed to produce a new number.
  3. This new number is divided by a large constant (to normalize it between 0 and 1).
  4. The process repeats for each call to Math.random(), generating the next number in the sequence.

This sequence is predictable if you know the seed, making it suitable for simulations or games but not for cryptographic purposes.

Why Isn’t Math.random() Truly Random?

Since Math.random() relies on a deterministic algorithm, the sequence of numbers can be replicated if the initial seed and algorithm are known. For critical applications like encryption, you need cryptographically secure random numbers, which can be generated using the Web Crypto API:

// Cryptographically secure random values
const array = new Uint32Array(5);
window.crypto.getRandomValues(array);
console.log(array);
Enter fullscreen mode Exit fullscreen mode

Why Randomness is Hard for Computers

hard

Computers operate on binary logic (0s and 1s), and randomness introduces uncertainty, which doesn't naturally align with deterministic systems. To simulate randomness:

  1. External Inputs: Many systems use external, unpredictable data (like mouse movements, keystrokes, or system clock) to generate initial seed values.
  2. Entropy Pool: Operating systems often maintain an entropy pool that gathers noise from various sources to improve randomness.

Conclusion

Randomness in computers is an illusion crafted using sophisticated algorithms and initial seeds. While Math.random() provides a convenient way to generate random numbers for most everyday tasks, it’s important to understand its limitations and deterministic nature. For tasks requiring true randomness or security, consider cryptographic methods.

Let’s embrace the fascinating interplay between determinism and randomness that powers our code!

Thank-you

Top comments (2)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

NOTHING is truly random. It's an abstract concept

Collapse
 
extinctsion profile image
Aditya

Yes!