mirror of
https://github.com/fediland/fediland.github.io.git
synced 2024-11-14 16:43:06 +00:00
32 lines
613 B
JavaScript
32 lines
613 B
JavaScript
/**
|
|
* Returns random item from given array.
|
|
*
|
|
* @param {array} array
|
|
* @returns {object}
|
|
*/
|
|
function getRandomItem(array) {
|
|
return array[array.length * Math.random() << 0];
|
|
}
|
|
|
|
/**
|
|
* Randomly sorts an array.
|
|
*
|
|
* @param {array} array
|
|
* @returns {array}
|
|
*/
|
|
function shuffleArray(array) {
|
|
return array.sort(function(a, b) {
|
|
return getRandomInt(1) ? 1 : -1;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Returns random number from 0 to max.
|
|
*
|
|
* @param {number} max
|
|
* @returns {number}
|
|
*/
|
|
function getRandomInt(max) {
|
|
return Math.floor(Math.random() * Math.floor(max + 1));
|
|
}
|