1
0
anagram-games/www/random.js

35 lines
951 B
JavaScript

'use strict';
/* Utilities for random numbers. */
// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
function getRandomBool() {
return getRandomInt(2) == 1;
}
function getRandomLetter() {
return (10 + getRandomInt(26)).toString(36);
}
// From https://stackoverflow.com/a/2450976
function shuffle(array) {
let currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}