The ids package provides randomly generated ids in a number of different forms with different readability and sizes.

Random bytes

The random_id function generates random identifiers by generating bytes random bytes and converting to hexadecimal (so each byte becomes a pair of characters). Rather than use R’s random number stream we use the openssl package here.

ids::random_id()
## [1] "20e33563839adc26d4c73ebe241f8032"

All ids functions take n as the first argument to be the number of identifiers generated:

ids::random_id(5)
## [1] "649d8b852867bf48d63d3149ed44792a" "c3c8b1516a40347dc99dc220a27442da"
## [3] "781d60e3711c53c46254133e517af094" "3b7ac1387b44cbd1ae5743f8861a0c33"
## [5] "dcf5d59add707b678233fecba9cd447b"

The default here is 16 bytes, each of which has 256 values (so 256^16 = 2^128 = 3.4e38 combinations). You can make these larger or smaller with the bytes argument:

ids::random_id(5, 8)
## [1] "1b55a49dba1c35db" "3523a46409ba29fc" "b12dfc63e9e6d28d" "d97a7fb2b0d61493"
## [5] "f0b798b471e873a2"

If NULL is provided as n, then a generating function is returned (all ids functions do this):

f <- ids::random_id(NULL, 8)
f
## function (n = 1) 
## {
##     random_id(n, bytes, use_openssl, global)
## }
## <bytecode: 0x564cb136e7d0>
## <environment: 0x564cb26445f0>

This function sets all arguments except for n

f()
## [1] "313a901a2cb2485c"
f(4)
## [1] "a904bfee70c47dc6" "4d70abe22e1a7ab6" "2583affea5717203" "e0851fd1cbe619ac"

UUIDs

The above look a lot like UUIDs but they are not actually UUIDs. The ids::uuid function generates “version 4” UUIDs which are almost entirely random but contain dashes and have particular bits set:

ids::uuid()
## [1] "58e46139-7364-471a-8e5c-13805ace98e1"

As above, generate more than one UUID:

ids::uuid(4)
## [1] "a16d837e-232b-4ce7-bb33-7f98463522e8"
## [2] "de4c2921-3331-45b4-8079-11e3f770930c"
## [3] "058081b1-42a1-4fcc-ac24-c8f03b255e83"
## [4] "b5c72ffb-b17b-432a-82df-4df5041537d2"

Adjective animal

Generate (somewhat) human readable identifiers by combining one or more adjectives with an animal name.

## [1] "careful_gaur"

The list of adjectives and animals comes from gfycat.com, via https://github.com/a-type/adjective-adjective-animal

Generate more than one identifier:

## [1] "unterrestrial_takin"      "illfated_bonobo"         
## [3] "wet_vixen"                "classless_africanwildcat"

Use more than one adjective for very long identifier

ids::adjective_animal(4, 3)
## [1] "changeable_tricolour_ethnological_shrimp"   
## [2] "zealous_preagricultural_snobbish_drafthorse"
## [3] "brilliant_rhombohedral_locustal_tattler"    
## [4] "waiting_necessary_dermatic_earwig"

There are 1748 animal names and 8946 adjectives so each one you add increases the identifier space by a factor of 8946. So for 1, 2, and 3 adjectives there are about 15.6 million, 140 billion and 1250 trillion possible combinations.

This is a much smaller space than the random identifiers above, but these are more readable and memorable.

By default the random numbers come from R’s random number stream so are affected by set.seed(); see below for details.

Because some of the animal and adjective names are very long (e.g. a quasiextraterritorial hexakosioihexekontahexaphobic queenalexandrasbirdwingbutterfly), in order to generate more readable/memorable identifiers it may be useful to restrict the length. Pass max_len in to do this.

ids::adjective_animal(4, max_len = 6)
## [1] "neon_beetle"   "dermal_quagga" "gluey_kakapo"  "slim_grison"

A vector of length 2 here can be used to apply to the adjectives and animal respectively:

ids::adjective_animal(20, max_len = c(5, Inf))
##  [1] "legal_watussi"            "baggy_husky"             
##  [3] "mirky_cranefly"           "lithe_langur"            
##  [5] "glass_tigerbeetle"        "testy_octopus"           
##  [7] "puny_robin"               "suede_mussel"            
##  [9] "bare_americanindianhorse" "lowly_mammal"            
## [11] "antsy_newfoundlanddog"    "wavy_basil"              
## [13] "wood_grayfox"             "dying_firecrest"         
## [15] "lazy_quoll"               "peat_trumpeterbird"      
## [17] "choky_easternglasslizard" "human_wuerhosaurus"      
## [19] "pagan_bittern"            "solid_fieldspaniel"

Note that this decreases the pool size and so increases the chance of collisions.

In addition to snake_case, the default, the punctuation between words can be changed to:

kebab-case:

ids::adjective_animal(1, 2, style = "kebab")
## [1] "residential-sizy-whiterhino"

dot.case:

ids::adjective_animal(1, 2, style = "dot")
## [1] "brave.selfdestroying.elephantseal"

camelCase:

ids::adjective_animal(1, 2, style = "camel")
## [1] "antiallergenicBioclimaticQueenalexandrasbirdwing"

PascalCase:

ids::adjective_animal(1, 2, style = "pascal")
## [1] "QuickMilitantKarakul"

CONSTANT_CASE (aka SHOUTY_CASE)

ids::adjective_animal(1, 2, style = "constant")
## [1] "DISCUSSIBLE_RELISHABLE_XOLOITZCUINTLI"

or with spaces, lower case:

ids::adjective_animal(1, 2, style = "lower")
## [1] "asphaltic savoury queenslandgrouper"

UPPER CASE

ids::adjective_animal(1, 2, style = "upper")
## [1] "ATMOSPHERIC SERENE RINGWORM"

Sentence case

ids::adjective_animal(1, 2, style = "sentence")
## [1] "Supersecure hardheaded mare"

Title Case

ids::adjective_animal(1, 2, style = "title")
## [1] "Absent Cycadaceous Huemul"

MocKiNg sPoNgEbOb CaSe

ids::adjective_animal(1, 2, style = "spongemock")
## [1] "cREdItAbLe-DiNKy-QueEnbEE"

Again, pass n = NULL here to create a generating function:

aa3 <- ids::adjective_animal(NULL, 3, style = "kebab", max_len = c(6, 8))

…which can be used to generate ids on demand.

aa3()
## [1] "blank-frowsy-dozing-johndory"
aa3(4)
## [1] "sand-bored-slow-anaconda"   "nylon-scared-sepia-flicker"
## [3] "fervid-dismal-sulfur-kid"   "wacky-binary-stingy-myna"

Random sentences

The sentence function creates a sentence style identifier. This uses the approach described by Asana on their blog. This approach encodes 32 bits of information (so 2^32 ~= 4 billion possibilities) and in theory can be remapped to an integer if you really wanted to.

ids::sentence()
## [1] "4_nutty_goats_hopping_slowly"

As with adjective_animal, the case can be changed:

ids::sentence(2, "dot")
## [1] "12.dazzling.hedgehogs.wandering.boastfully"  
## [2] "17.awesome.platypuses.jumping.optimistically"
ids::sentence(2, "spongemock")
## [1] "21-UnKeMpt-rEiNdeErS-pRaNcInG-qUIrKiLy"    
## [2] "6-cOoRdINatEd-FlAmInGoS-sAUnTeRiNg-brAvElY"

If you would rather past tense for the verbs, then pass past = TRUE:

ids::sentence(4, past = TRUE)
## [1] "26_amazing_moose_laughed_zestfully"     
## [2] "7_curious_sheep_pranced_quietly"        
## [3] "31_waggish_iguanas_squiggled_youthfully"
## [4] "7_nutty_llamas_sauntered_shakily"

proquints

“proquints” are an identifier that tries to be information dense but still human readable and (somewhat) pronounceable; “proquint” stands for PRO-nouncable QUINT-uplets. They are introduced in https://arxiv.org/html/0901.4016

ids can generate proquints:

ids::proquint(10)
##  [1] "zabir-fagug" "zosut-mavig" "ribis-liduv" "jihon-tobur" "dizol-figab"
##  [6] "kosir-bunat" "buhiv-vumar" "tadis-vunaz" "jahor-kagan" "tutod-hisin"

By default it generates two-word proquints but that can be changed:

ids::proquint(5, 1)
## [1] "tomuh" "vomul" "ralon" "zuzok" "sogak"
ids::proquint(2, 4)
## [1] "mapig-kunod-pijoh-zujis" "vodap-bumad-notug-rirup"

Proquints are formed by alternating consonant/vowel/consonant/vowel/consonant using a subset of both (16 consonants and 4 vowels). This yields 2^16 (65,536) possibilities per word. Words are always lower case and always separated by a hyphen. So with 4 words there are 2^64 combinations in 23 characters.

Proquints are also useful in that they can be translated with integers. The proquint kapop has integer value 25258

ids::proquint_to_int("kapop")
## [1] 25258
ids::int_to_proquint(25258)
## [1] "kapop"

This makes proquints suitable for creating human-pronounceable identifiers out of things like ip addresses, integer primary keys, etc.

The function ids::int_to_proquint_word will translate between proquint words and integers (and are vectorised)

w <- ids::int_to_proquint_word(sample(2^16, 10) - 1L)
w
##  [1] "vugir" "fivoh" "pogop" "jomab" "pofos" "zidif" "zakik" "juraz" "barip"
## [10] "zunok"

and ids::proquint_word_to_int does the reverse

##  [1] 60635 10148 43242 23040 43180 62546 61846 24271   730 65126

while ids::proquint_to_int and ids::int_to_proquint allows translation of multi-word proquints. Overflow is a real possibility; the maximum integer representable is only about r human_no(.Machine$integer.max) and the maximum floating point number of accuracy of 1 is about 9010 trillion – these are big numbers but fairly small proquints:

ids::int_to_proquint(.Machine$integer.max - 1)
## [1] "luzuz-zuzuv"
ids::int_to_proquint(2 / .Machine$double.eps)
## [1] "babob-babab-babab-babab"

But if you had a 6 word proquint this would not work!

p <- ids::proquint(1, 6)

Too big for an integer:

## Error in proquint_combine(idx, len, as): Numeric overflow: cannot represent proquint as numeric

And too big for an numeric number:

ids::proquint_to_int(p, as = "numeric")
## Error in proquint_combine(idx, len, as): Numeric overflow: cannot represent proquint as numeric

To allow this, we use openssl’s bignum support:

ids::proquint_to_int(p, as = "bignum")
## [[1]]
## [b] 68240143167322810041142066081

This returns a list with one bignum (this is required to allow vectorisation).

Roll your own identifiers

The ids functions can build identifiers in the style of adjective_animal or sentence. It takes as input a list of strings. This works particularly well with the rcorpora package which includes lists of strings.

Here is a list of Pokemon names:

pokemon <- tolower(rcorpora::corpora("games/pokemon")$pokemon$name)
length(pokemon)
## [1] 663

…and here is a list of adjectives

adjectives <- tolower(rcorpora::corpora("words/adjs")$adjs)
length(adjectives)
## [1] 961

So we have a total pool size of about 637 thousand, which is not huge, but it is at least topical.

To generate one identifier:

ids::ids(1, adjectives, pokemon)
## [1] "conceptual_venomoth"

All the style-changing code is available:

ids::ids(10, adjectives, pokemon, style = "dot")
##  [1] "lacklustre.cradily"     "retiring.cradily"       "quick.mankey"          
##  [4] "exponential.pikachu"    "unwary.magnemite"       "primer.gliscor"        
##  [7] "eaten.vibrava"          "indiscriminate.victini" "sentient.vibrava"      
## [10] "flammable.uxie"

Better would be to wrap this so that the constants are not passed around the whole time:

adjective_pokemon <- function(n = 1, style = "snake") {
  pokemon <- tolower(rcorpora::corpora("games/pokemon")$pokemon$name)
  adjectives <- tolower(rcorpora::corpora("words/adjs")$adjs)
  ids::ids(n, adjectives, pokemon, style = style)
}

adjective_pokemon(10, "kebab")
##  [1] "boundary-poliwag"          "bohemian-lucario"         
##  [3] "contemporaneous-cresselia" "process-ampharos"         
##  [5] "commuting-liepard"         "healthiest-eevee"         
##  [7] "inert-stunfisk"            "cardinal-lopunny"         
##  [9] "employed-whimsicott"       "running-regirock"

As a second example we can use the word lists in rcorpora to generate identifiers in the form <mood>_<scientist>, like “melancholic_darwin”. These are similar to the names of docker containers.

First the lists of names themselves:

moods <- tolower(rcorpora::corpora("humans/moods")$moods)
scientists <- tolower(rcorpora::corpora("humans/scientists")$scientists)

Moods include:

sample(moods, 10)
##  [1] "great"        "haughty"      "threatened"   "jaded"        "sinful"      
##  [6] "thoughtful"   "delighted"    "dependent"    "conventional" "dirty"

The scientists names contain spaces which is not going to work for us because ids won’t correctly translate all internal spaces to the requested style.

sample(scientists, 10)
##  [1] "emil fischer"              "frederick gowland hopkins"
##  [3] "pierre-simon laplace"      "alexander fleming"        
##  [5] "b. f. skinner"             "stephen hawking"          
##  [7] "ernest rutherford"         "james dwight dana"        
##  [9] "antoine lavoisier"         "bill nye"

To hack around this we’ll just take the last name from the list and remove all hyphens:

scientists <- vapply(strsplit(sub("(-|jr\\.$)", "", scientists), " "),
                     tail, character(1), 1)

Which gives strings that are just letters (though there are a few non-ASCII characters here that may cause problems because string handling is just a big pile of awful)

sample(scientists, 10)
##  [1] "gamow"    "planck"   "nye"      "haxel"    "fermi"    "binet"   
##  [7] "halley"   "drexler"  "tombaugh" "wundt"

With the word lists, create an identifier:

ids::ids(1, moods, scientists)
## [1] "enraged_sagan"

Or pass NULL for n and create a function:

sci_id <- ids::ids(NULL, moods, scientists, style = "kebab")

which takes just the number of identifiers to generate as an argument

sci_id(10)
##  [1] "giddy-carver"        "splendid-dirac"      "reminiscent-hirase" 
##  [4] "private-tyson"       "uplifted-buffon"     "sabotaged-lucretius"
##  [7] "contempt-czerny"     "outraged-humboldt"   "dignified-napier"   
## [10] "awake-bosch"

Relationship to random number generation

Creating random identifiers requires some source of random numbers. There are a couple of competing needs here:

  1. You might want the identifiers to be repeatable, so that setting the seed will ensure the same stream of random identifiers:
## [1] "careful_annelida"      "uncalorific_ivorygull" "marginal_tegus"
## [1] "careful_annelida"      "uncalorific_ivorygull" "marginal_tegus"
  1. Alternatively you might want to use identifiers as “globally unique” keys, avoiding any collisions
set.seed(1)
ids::random_id(3, 4)
## [1] "a414de2e" "b6ac1212" "2a2b3f24"
set.seed(1)
ids::random_id(3, 4)
## [1] "e48259b0" "9ceeda32" "1858a487"

To support this, ids uses two different types of sources of random numbers:

  • Global random numbers come from R’s random number stream and respond to set.seed, as above. These are used by default in ids::adjective_animal, ids::proquint, ids::sentence (i.e., the human-readable ids functions), and can be selected if required for ids::random_id and ids::uuid by passing the option global = TRUE.
  • Non-global random numbers come from “some other source”, either openssl::rand_num or an internal source if openssl is unavailable (see below). These will not be affected by set.seed and we provide no way of resetting or reproducing the stream. This is the default for ids::random_id and ids::uuid (i.e., the non-reproducible ids functions), and can be selected if required by ids::proquint, ids::adjective_animal and ids::sentence by passing the option global = FALSE.

For non-global random numbers, all else being equal, you should prefer to use the numbers provided by openssl. The default for all functions is to use openssl if available. You can force openssl by passing use_openssl = TRUE (perhaps along with global = FALSE); if the package is not installed then we will error rather than continuing with the internal random number stream.

The internal random number stream is a pure-R implementation of the xoroshiro128+ algorithm. As such it is quite slow (about 50x slower per byte of output than the cryptographically secure numbers from openssl) but it always available. We seed the stream by hashing (via utils::md5sum) the current process id and the system time to the maximum precision available on that platform.