Random hexadecimal identifiers. If possible, by default this uses the openssl package to produce a random set of bytes, and expresses that as a hex character string, creating cryptographically secure (unpredictable) identifiers. If that is unavailable, fall back on the xoshiro128+ algorithm to produce random numbers that are not cryptographically secure, but which do not affect the global random number stream (see Details). If desired, you can produce "predictable" random identifiers that respect the value of the global random number stream via set.seed.

random_id(n = 1, bytes = 16, use_openssl = NULL, global = FALSE)

Arguments

n

number of ids to return. If NULL, it instead returns the generating function

bytes

The number of bytes to include for each identifier. The length of the returned identifiers will be twice this long with each pair of characters representing a single byte.

use_openssl

Optionally a logical, indicating if we should use the openssl for generating the random identifiers from the non-global source. If not given we prefer to use openssl if it is available but fall back on R (See Details).

global

Logical, indicating if random numbers should be global (given R's global random number seed). If TRUE, then ids generated will be predictable.

Details

Since ids version 1.2.0, the openssl package is optional, and this affects non-global random number drawing. If you have openssl installed your random numbers will be ~50x faster than the implementation we include here.

If global = TRUE we always use a simple sample based algorithm that is driven from the global random number stream. However, when global = FALSE the behaviour depends on the value of use_openssl and whether that package is installed, either using the openssl generators, using an internal algorithm based on xoshiro128+ or erroring.

  • use_openssl = NULL and openssl installed: openssl

  • use_openssl = NULL and openssl missing: internal

  • use_openssl = TRUE and openssl installed: openssl

  • use_openssl = TRUE and openssl missing: error

  • use_openssl = FALSE: internal

Author

Rich FitzJohn

Examples

# Generate a random id:
ids::random_id()
#> [1] "b78a7130d790a6b28930d30b6cfa379e"

# Generate 10 of them!
ids::random_id(10)
#>  [1] "ac1249c4e16dcaeb953b837df72c6d86" "3c9d51a5a1be645e1bc997f0951699e9"
#>  [3] "aa8beac87dbb55b9b5a2b20d36fbef8c" "f6e2b94e9c9c5284ab429c4e2ba53316"
#>  [5] "1c01cdc4161656be25c3dc106c7aad42" "a276a4aa4bda9778f4f10c63a1742ea3"
#>  [7] "8591f8b473aeca8e77703c373d637a81" "9e1ea2b1dc4afbaac140ed5b11b53ddd"
#>  [9] "c50ae6bc1052de17c60c8a2d6dda5fb9" "b6e0547f9726089b1ae526e7dc08e48f"

# Different length ids
random_id(bytes = 8)
#> [1] "d1ec0ec4a8534fd1"
# (note that the number of characters is twice the number of bytes)

# The ids are not affected by R's RNG state:
set.seed(1)
(id1 <- ids::random_id())
#> [1] "25c3653470d77b200580003d828a8dd0"
set.seed(1)
(id2 <- ids::random_id())
#> [1] "5a19775806c3c0ca01671b4e1439cbe8"
# The generated identifiers are different, despite the seed being the same:
id1 == id2
#> [1] FALSE

# If you need these identifiers to be reproducible, pass use_openssl = FALSE
set.seed(1)
(id1 <- ids::random_id(use_openssl = FALSE))
#> [1] "3c93ad914fd3fd2be1eefd3a42c401bc"
set.seed(1)
(id2 <- ids::random_id(use_openssl = FALSE))
#> [1] "baa499639163de69df42b9d1cb9a068d"
# This time they are the same:
id1 == id2
#> [1] FALSE

# Pass `n = NULL` to generate a function that binds your arguments:
id8 <- ids::random_id(NULL, bytes = 8)
id8(10)
#>  [1] "bcbe3c6f549aa9af" "af7f0f90dcb560ba" "58f2788428b28acc" "a9402bdedc1e1a4d"
#>  [5] "79f11f16352b551f" "ca6af64d14fa94a5" "192610e8372508bd" "fbed07ac676d8dfb"
#>  [9] "3952374aba519616" "841dcc6203573eba"