ช่วง: [15, 26] จำนวนครั้ง: 10 สุ่ม 10 ตัว หาค่ามากที่สุด
ตัวอย่างโค้ด
const range = [15, 26];
const count = 10;
const randomBetweenRange = ([min, max]) => {
const random = Math.random() * (max - min) + min;
return random;
};
const pickGreatestRandom = (range, count) => {
const res = [];
for (let i = 0; i < count; i++) {
const random = randomBetweenRange(range);
res.push(random);
}
return Math.max(...res);
};
console.log(pickGreatestRandom(range, count));ผลลัพธ์
25.686387806628826
คำอธิบาย: Math.random() * (max - min) + min สุ่ม float ในช่วง [min, max) วนลูป count ครั้ง เก็บในอาร์เรย์ Math.max(...res) หาค่าสูงสุด
หมายเหตุ: ผลลัพธ์สุ่มต่างกันทุกครั้งที่รัน