Search code examples
rrandomgambling

runif() is not uniform


I'm writing a roulette simulator and I stuck just on the beginning. I wanted to draw integer from 0 to 36, so I used runif(). I noticed that 0's are outstanding. Have a look:

n=1000000
x=floor(runif(n,0,37))
hist(x,breaks=37)

Histogram: floor(runif(n,0,37))

To remove "0's" i wrote:

n=1000000
x=floor(runif(n,0,37)*100)/100
hist(x,breaks=37)

What gave me

Histogram: enter image description here

And my question is why it works?


Solution

  • No, it's not a problem with runif.

    Try this instead: plot(density(x))

    and you see the distribution is smooth

    the issue is with where the breaks in your histogram are being placed, and that there's a fencepost problem at work. Histogram is not the best visualization tool for this, because basically the breaks have to line up perfectly.