mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
21 lines
476 B
JavaScript
21 lines
476 B
JavaScript
import _ from 'lodash';
|
|
|
|
function trueRandom () {
|
|
return Math.random();
|
|
}
|
|
|
|
// Get a random property from an object
|
|
// returns random property (the value)
|
|
module.exports = function randomVal (obj, options = {}) {
|
|
let array = options.key ? _.keys(obj) : _.values(obj);
|
|
let random = options.predictableRandom || trueRandom();
|
|
|
|
array.sort();
|
|
|
|
let randomIndex = Math.floor(random * array.length);
|
|
|
|
return array[randomIndex];
|
|
};
|
|
|
|
module.exports.trueRandom = trueRandom;
|