Based on mdn Math.random() can return 0 but can not return 1
The Math.random() static method returns a floating-point, pseudo-random number that's greater than or equal to 0 and less than 1
I tried to loop Math.random() to get 10 number below e-10 and it takes 15 minutes to complete it with my 4 cores 8 threads cpu and from that 10 numbers 0 never get returned. Is my test case just too small or latest javascript Math.random() can never return 0?
note: I run the code using node.js v18.12.1 and the smallest number ever returned is 5.2724491439448684e-12 (yes its small, but it isn't 0)
let countSmallNumber = 0;
let temp = 0;
console.time("loopTime");
while (countSmallNumber < 10) {
temp = Math.random();
if (temp < 0.0000000001) {
countSmallNumber++;
console.log(`almost 0 => ${temp}`);
}
}
console.timeEnd("loopTime");
Aucun commentaire:
Enregistrer un commentaire