What is Randomness in Javascript?

It is impossible in computing to generate completely random numbers. This is because every calculation inside a computer has a logical basis of cause and effect, while random events don’t follow that logic.

Computers are not capable of creating something truly random. True randomness is only possible through a source of external data that a computer cannot generate, such as the movement of many lava lamps at once (which has been used as an unbreakable random encryption in the real world), meteographic noise, or nuclear decay.

The solution that Javascript, and other programming languages, use to implement randomness is “pseudo-random” number generation. Javascript random numbers start from a hidden internal value called a “seed.” The seed is a starting point for a hidden sequence of numbers that are uniformly distributed throughout their possible range.

Developers cannot change Javascript’s pseudo-random seed or the distribution of values in its generated pseudo-random sequences. Different Javascript implementations and different browsers often start with different seeds. Do not assume that different browsers, or even different computers, will always use the same seed.

Javascript random numbers are not safe for use in cryptography because deciphering the seed could lead to decryption of the hidden number sequence. Some functions exist to create cryptographically secure pseudo-random numbers in Javascript, but they are not supported by older browsers.

In this blog post, we’ll first cover the canonical methods of creating Javascript random numbers. Then we’ll move onto ways of obtaining higher-quality random data; your needs will determine the worthwhile effort.

Generating Javascript Random Numbers

Javascript creates pseudo-random numbers with the function Math.random(). This function takes no parameters and creates a random decimal number between 0 and 1. The returned value may be 0, but it will never be 1.

let value1 = Math.random();

You can use Math.random() to create whole numbers (integers) or numbers with decimals (floating point numbers). Since Math.random() creates floating point numbers by default, you can create a random floating point number simply by multiplying your maximum acceptable value by the result from Math.random(). Therefore, to create a pseudo-random number between 0 and 2.5:

let value2 = Math.random() * 2.5;

Creating a pseudo-random integer is a little more difficult; you must use the function Math.floor() to round your computed value down to the nearest integer. So, to create a random number between 0 and 10:

let value3 = Math.floor(Math.random() * 10);

Generating Javascript Random Numbers More Easily

Math.random() is a useful function, but on its own it doesn’t give programmers an easy way to generate pseudo-random numbers for specific conditions. There may be a need to generate random numbers in a specific range that doesn’t start with 0, for example.

Fortunately, there are simple Javascript functions that programmers can create to make pseudo-random numbers more manageable. The rest of this section will show you how to create those functions, then put them all together into a single pseudo-random number generator.

Integer Pseudo-Random Numbers Across A Range

In the previous examples, Math.random() could never create a number at the very top of a specified range. If you wanted a number between 0 and 5, for example, you could get 0-4, but never 5. The solution to this problem, if you’re creating an integer, is adding 1 to the result.

// Generate a number between 0 and 10, including 10

function generateRandomInteger(max) {

    return Math.floor(Math.random() * max) + 1;

}
let value4 = generateRandomInteger(10);

Since floating point numbers in Javascript go out to many decimal places, there isn’t a simple one-number solution to include the maximum possible floating point number in your range unless you want to make shaky assumptions and type a lot of zeroes. Instead, you can use some simple math that also works with integers to get pseudo-random numbers all across your range.

// Generate a random number between 2 and 10, including both 2 and 10

function generateRandomIntegerInRange(min, max) {

    return Math.floor(Math.random() * (max - min + 1)) + min;

}

let value5 = generateRandomIntegerInRange(2, 10);

Hüseyin Özdemir

Designbees – Praktikant 2023