A Summary of Random Number Generation in JavaScript
TL;DR
JavaScript's random functionality is quite inconvenient, so here's a summary of options and commonly used functions.
Random Basics
Math.random
The simplest approach is to use Math.random. This function generates a random number in the range [0, 1]. However, it doesn't allow specifying a seed value, which is a drawback.
Slightly More Useful Functions
XorShift
XorShift is a simple algorithm that supports seed values and produces relatively good random numbers.
Mersenne Twister
You can use the Mersenne Twister by using the external library mt.js. Here's how it looks. You can also specify a seed using the setSeed method.
Shuffling Arrays
The algorithms above work fine for basic random number generation, but sometimes you need to generate random numbers without duplicates. In that case, you can simply shuffle an array. Shuffling can be done using the Fisher-Yates shuffle or Durstenfeld's algorithm. Durstenfeld's approach is said to be faster.
The random parameter is designed to accept any function that takes a max value and returns a random number. A simple implementation would be:
Fisher-Yates Shuffle
Durstenfeld's Algorithm
Almost the same, but: