Skip to content

Instantly share code, notes, and snippets.

@njtierney
Created September 21, 2020 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save njtierney/29130b7ee750b0d02b1113c11d453383 to your computer and use it in GitHub Desktop.
Save njtierney/29130b7ee750b0d02b1113c11d453383 to your computer and use it in GitHub Desktop.
Function to generate random alpha numeric strings of a given length. Used to generate random API keys
alpha_num <- function(n){
  
  pool <- c(letters,
            LETTERS,
            rep(0:9, length.out = length(letters)))
  
  paste0(
  sample(x = pool,
         size = n,
         replace = TRUE),
  collapse = ""
  )
}

alpha_num(1)
#> [1] "K"
alpha_num(10)
#> [1] "ur6Qo7eRr0"
alpha_num(20)
#> [1] "qKNLbPwQrrkDwugB8AAS"

Created on 2020-09-21 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment