mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-14 13:17:24 +01:00
20 lines
485 B
JavaScript
20 lines
485 B
JavaScript
import values from 'lodash/values';
|
|
import keys from 'lodash/keys';
|
|
|
|
export function trueRandom () {
|
|
return Math.random();
|
|
}
|
|
|
|
// Get a random property from an object
|
|
// returns random property (the value)
|
|
export default function randomVal (obj, options = {}) {
|
|
const array = options.key ? keys(obj) : values(obj);
|
|
const random = options.predictableRandom || trueRandom();
|
|
|
|
array.sort();
|
|
|
|
const randomIndex = Math.floor(random * array.length);
|
|
|
|
return array[randomIndex];
|
|
}
|