August 4, 2018
2 min read
In game development, especially demos where I'm not using a full library or framework, a very useful utility function is to randomly pick from a set of options. I usually accomplish this by defining an array of the options and using this utility function to randomly choose one.
See it in action in this demo: https://codepen.io/rmkubik/pen/rrKZYr
function pickRandomlyFromArray(array) { return array[Math.floor(Math.random() * array.length)];}
// You can also bind it to the Array prototype for easy referenceArray.prototype.pickRandom = function() { return pickRandomlyFromArray(this);}// Then use it on an anonymous array[1, 2, 3].pickRandom();
const arr = [4, 5, 6];// Call it from the Array prototypearr.pickRandom();// Pass the array into pick functionpickRandomlyFromArray(arr);