habitica/website/common/script/libs/randomVal.js

22 lines
516 B
JavaScript
Raw Normal View History

import values from 'lodash/values';
import keys from 'lodash/keys';
2016-03-08 17:58:39 +00:00
function trueRandom () {
return Math.random();
}
2016-09-26 13:14:18 +00:00
// 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();
2016-03-08 17:58:39 +00:00
array.sort();
let randomIndex = Math.floor(random * array.length);
return array[randomIndex];
2016-03-08 17:58:39 +00:00
};
2016-10-21 03:03:15 +00:00
module.exports.trueRandom = trueRandom;