So I want to make a number selector in JS that select a ROUND number between 1 and 100. Like selecting a winning ticket for example.
I found out I could use a function and then Math.round, like this
function sort(min,max) {
return Math.round(Math.randon() * (max - min) min)
}
console.log(sort(1,100))
But then I readed Math.round will make a non-even distribution, with max and min values less likely to roll than the others.
So how can I solve this one? Getting a ROUND number between 1 and 100 with even distribution?
CodePudding user response:
Sort is not a good name for random number picker. Anyway, looks pretty even to me (if you use floor)
function rand_int(min, max) {
return Math.floor(Math.random() * (max - min 1) min)
}
var bucket = {}
for (var i = 0; i < 1000000; i ) {
var x = rand_int(1, 10)
bucket[x] = (bucket[x] || 0) 1
}
console.log(bucket)