[R] runif limited precision

Duncan Murdoch murdoch at stats.uwo.ca
Fri Jan 2 22:03:49 CET 2009


On 02/01/2009 2:45 PM, Stavros Macrakis wrote:
> runif appears to give 31 bits of precision, but this isn't mentioned
> in the documentation page. The R numeric type supports 53 digits of
> precision, and other numeric functions (sin, etc.) give full-precision
> results.  So I'd think that either runif should give full precision or
> its documentation should mention this limitation.

It refers to the .Random.seed page for details, and that page mentions 
the limitation to at most 2^32 different values for most of the generators.

> 
> #integers
> table(runif(10000,-2^30,2^30) %% 1)
>    0  0.5
> 4972 5028

Your interval is 2^31 units long, and you're getting values on the 
integers and halfway between, so that looks like 2^32 bits, not 2^31.

> #natural numbers
>> table(runif(10000,0,2^31) %% 1)
>    0  0.5
> 4956 5044
> #fractions
> table((r<-runif(10000,0,1)*2^31)-floor(r))
>    0  0.5
> 5021 4979

Same here.

Duncan Murdoch

> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list