Skip to content

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()

Value

a integer64 vector

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_.

See also

Examples

  runif64(12)
#> integer64
#>  [1] 2203486589999907662  -7390437899354248509 -6762436007047881691
#>  [4] -6066442108870003601 -3352573508150169028 -7211614202959724681
#>  [7] 2886211095346392419  2849469864900762174  -5475890359380813228
#> [10] 8245139917399175507  -387287894727004678  -8779821175653318524
  runif64(12, -16, 16)
#> integer64
#>  [1] 7   6   -12 -7  3   9   13  -16 -2  0   8   -7 
  runif64(12, 0, as.integer64(2^60)-1)  # not 2^60-1 !
#> integer64
#>  [1] 108260134087860730  206504036133322050  167894928368271249 
#>  [4] 217266181698945152  68238513265304626   803990508688581509 
#>  [7] 804030871938955553  136546609812179767  1039986796350529502
#> [10] 1081777040808299882 21402243635917267   613824368535526288 
  var(runif(1e4))
#> [1] 0.08329483
  var(as.double(runif64(1e4, 0, 2^40))/2^40)  # ~ = 1/12 = .08333
#> [1] 0.0829365

  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))
#> 
#>  1  2  3  7  8 10 11 12 13 16 
#>  1  2  3  1  1  1  2  2  1  2 
  table(sample(16, replace=TRUE))
#> 
#>  1  2  3  4  5 10 11 13 14 15 16 
#>  2  1  1  2  1  1  2  1  3  1  1 
  table(runif64(16, 1, 16, replace=TRUE))
#> 
#>  4  6  7  8 11 14 15 16 
#>  3  1  1  4  2  2  2  1