Create uniform random 64-bit integers within defined range
Usage
runif64(
n,
min = lim.integer64()[1L],
max = lim.integer64()[2L],
replace = TRUE
)
Arguments
- n
length of return vector
- min
lower inclusive bound for random numbers
- max
upper inclusive bound for random numbers
- replace
set to FALSE for sampleing from a finite pool, see
sample()
Details
For each random integer we call R's internal C interface unif_rand()
twice.
Each call is mapped to 2^32 unsigned integers. The two 32-bit patterns are
concatenated to form the new integer64. This process is repeated until the
result is not a NA_INTEGER64_
.
Examples
runif64(12)
#> integer64
#> [1] 2915658457812729556 -3491511829406696631 -3496825464828931785
#> [4] 5395852077839172502 -7835456012596746149 -1516745570522234364
#> [7] -6832279058283761012 3129987699866411399 -1425688589184971166
#> [10] 1743351634359137850 -619044005758770366 -8343468749812013056
runif64(12, -16, 16)
#> integer64
#> [1] 9 -9 13 8 -10 0 5 14 9 3 -7 -2
runif64(12, 0, as.integer64(2^60)-1) # not 2^60-1 !
#> integer64
#> [1] 896499375407589580 799359046121395853 960877807272653955
#> [4] 733002594872096801 750278469790426764 288656285354523678
#> [7] 138536448177156874 133560880250517603 449130193256833291
#> [10] 972719265714589958 9862525313230357 403081621054673484
var(runif(1e4))
#> [1] 0.08441354
var(as.double(runif64(1e4, 0, 2^40))/2^40) # ~ = 1/12 = .08333
#> [1] 0.08380791
table(sample(16, replace=FALSE))
#>
#> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#> 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
table(runif64(16, 1, 16, replace=FALSE))
#>
#> 2 3 4 5 8 10 11 12 14 15 16
#> 1 2 1 1 1 1 1 3 2 1 2
table(sample(16, replace=TRUE))
#>
#> 1 3 6 8 9 10 12 13 14 15 16
#> 2 1 2 1 1 1 1 2 2 1 2
table(runif64(16, 1, 16, replace=TRUE))
#>
#> 1 2 5 7 8 10 12 13 14 16
#> 1 2 2 1 1 2 2 1 2 2