Skip to content

Instantly share code, notes, and snippets.

@cbare
Last active January 11, 2019 10:07
Show Gist options
  • Save cbare/5979354 to your computer and use it in GitHub Desktop.
Save cbare/5979354 to your computer and use it in GitHub Desktop.
A snippet of R code to generate a version 4 (random) UUID.
## Version 4 UUIDs have the form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
## where x is any hexadecimal digit and y is one of 8, 9, A, or B
## e.g., f47ac10b-58cc-4372-a567-0e02b2c3d479
uuid <- function(uppercase=FALSE) {
hex_digits <- c(as.character(0:9), letters[1:6])
hex_digits <- if (uppercase) toupper(hex_digits) else hex_digits
y_digits <- hex_digits[9:12]
paste(
paste0(sample(hex_digits, 8, replace=TRUE), collapse=''),
paste0(sample(hex_digits, 4, replace=TRUE), collapse=''),
paste0('4', paste0(sample(hex_digits, 3, replace=TRUE), collapse=''), collapse=''),
paste0(sample(y_digits,1), paste0(sample(hex_digits, 3, replace=TRUE), collapse=''), collapse=''),
paste0(sample(hex_digits, 12, replace=TRUE), collapse=''),
sep='-')
}
@heeseonhan
Copy link

heeseonhan commented Jan 11, 2019

Thanks your post.
How can i use it for api service.

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