Если вы помещаете случайные числа в массив, вы можете сделать это кратко с помощью Array.from
:
const random = () => Math.random();
const arr = Array.from({ length: 3 }, random);
console.log(arr);
Вы можете сделать то же самое для нескольких отдельных переменных путем деструктурирования:
const random = () => Math.random();
const [a, b, c] = Array.from({ length: 3 }, random);
console.log(a);
console.log(b);
console.log(c);
Если вам нужно позвонить random
с определенными параметрами, то:
const random = (low, high) => Math.floor((high - low) * Math.random()) + low;
const [a, b, c] = Array.from({ length: 3 }, () => random(1, 4));
console.log(a);
console.log(b);
console.log(c);