for (let i = 0; i < 999; i++) {
console.log("length-result:", (Math.random() + "").slice(2).length)
}
If you execute this code snippet, you will find that most of the results are less than 18.
However, theoretically speaking, the last digit of the result of random() only has a ten-percent opportunity to be 0. It means, only ten percents of the length-results can be less than 18.
Live snippet:
// Why are results of length 20 rare, rather than occurring ~90% of the time?
for (let i = 0; i < 999; i++) {
const str = Math.random() + '';
tbody.insertAdjacentHTML('beforeend', `<tr><td>${str.length}</td><td>${str}</td></tr>`);
}
td {
padding-right: 20px;
}
<table>
<tbody id="tbody">
</tbody>
</table>
Can you tell me what caused this behavior of Math.random?
Aucun commentaire:
Enregistrer un commentaire