Just last week, during my third project at Flatiron School in Seattle, I was creating a BlackJack App with my classmate Billy Witherspoon (If you want to try it out, you can play HERE. We used the Math.random() built in method of JavaScript to incorporate a shuffling function into our App. This got me thinking...if a computer is designed to extremely predictable and do exactly as it is told, how does a computer generate a random number? It can't TRULY be random, could it? I decided to look into Math.random() function to uncover what is going on behind the scenes.

#### Math.random()

SPOILER! the Math.random() function does NOT generate a random number. Although there is no truly random number generator in programming, JavaScript(actually browsers) does a pretty good job of simulating a random number. So what is doing? What it boils down to is JavaScript defines the rules, and it is up to the browser to decide how to generate the number. These are the rules that ECMAScript laid out

*[Math.random] Returns a Number value with positive sign, greater than or equal to 0 but less than 1, chosen randomly or pseudo randomly with approximately uniform distribution over that range, using an implementation-dependent algorithm or strategy. This function takes no arguments.
Each Math.random function created for distinct code Realms must produce a distinct sequence of values from successive calls.*

#### XORshift

Up until 2015, different browsers used different algorithms to conform to the standard set by ECMAScript. In 2015, all major browsers adopted a pseudo random number generator (PRNG) called xorshift128+.

So what the heck is going on in this function? Without diving into the details too much, essentially this function is taking in two seed values and performing shifting at the bit level. The *<<* and *>>* operators are left and right-shift, respectively. In an example where the seed values may be 25 << 2, the computer takes a binary representation of 25, then shifts it 2 places to the left, resulting in a new number (100). You can visualize an example below.

You might also be wondering what the =^ symbol represents. This is the XOR assignment operator. XOR (meaning exclusive or) compares the binary representations of two numbers and outputs a 0 where corresponding bits match or a 1 where bits are different. Therefore, it takes our two seed value binary representations and runs it through the XOR logic gate, producing a new number based on the seed values that were shift.

To simplify, the algorithm takes two seed values that the browser determines, switches them with the '<<' and '>>' operators to produce two different binary representations, then sends the two binary numbers through a XOR logic gate to produce our final 'random' number. Even though it isn't *truly* random, it most-often meets the needs of programmers. If you're trying to generate a truly number, you need to seed the value with a number generated outside of the computer...such as the EXACT time a user action occurs, an example being .3567 seconds after they click a button. This makes the process very unpredictable and impossible to reverse engineer.

#### In Summary

- JavaScript defines the rules of Math.random(), then it is up to the browser to decide how to accomplish this.
- Most modern browsers use the xorshift128+ method to generate a random number
- xorshift method takes in two seed values, switches them to binary, shifts around their value and then pushes these new values through a XOR gate to produce our final random number.
- To generate a true random number, the seed values must be generated outside of the computer.

## Top comments (0)