# Generation of Unique ID in R

Hello Everyone,
Can some help with how to generate a unique 6 digit URN in R,as I don't know how to do this please. Below are the rule for the URN

1. It needs to be alphanumeric,start with letter and maybe end with letter (e.g AA34YB)
2. Use only upper case alphabets
3. Do not use the alphabets O or I (this is the alphabet after H and before J)
4. Use only digits from 1- 9. Exclude 0
5. First two digit should be letter,then followed by 2 digit number and end with 2 digit letter,e.g "AA22DD","EE34TY","ER67YU"
6. All records must contain number as shown in rule 5
7. IT MUST BE 6 DIGIT PLEASE

I would love to generate upto 4 million unique records please.Any R code suggestion is highly welcome.I am not an expert in R,actually new to R

I would love to generate upto 4 million unique records please.Any R code suggestion is highly welcome

I have tried following but it does not produce the output I need

tt <- c(65:72,74:78,80:90)
replicate(10,rawToChar(as.raw(c(sample(tt, 1), sample(c(tt,49:57), 5, replace = TRUE)))))

replicate(10, paste0(c(sample(LETTERS[!LETTERS %in% c("O", "I")], 1),
sample(c(LETTERS[!LETTERS %in% c("O", "I")], 1:9), 4),
sample(LETTERS[!LETTERS %in% c("O", "I")], 1)), collapse = ''))

myletters <- LETTERS[!LETTERS %in% c("O", "I")]
mydigits <- 1:9
get_key <- function(l, d){

paste0(sample(l, 1), sample(c(l, d), 2, replace = TRUE), collapse = "")

}

replicate(100, get_key(myletters, mydigits))

Well, it's not that nice to expect someone to solve your problem for you without you at least doing the bare minimum and coming up with some solution that we can improve/suggest changes to.

R has `LETTERS` defined, so you can use that and `1:9` to get your alphabet and then create as many combinations as you want with `sample`. I would just go for brute-force and generate lots and lots of combinations randomly and then run them through `dplyr::distinct` to get unique IDs.

My bad,I have tired below but nit giving me what i want :
tt <- c(65:72,74:78,80:90)
replicate(10,rawToChar(as.raw(c(sample(tt, 1), sample(c(tt,49:57), 5, replace = TRUE)))))

replicate(10, paste0(c(sample(LETTERS[!LETTERS %in% c("O", "I")], 1),
sample(c(LETTERS[!LETTERS %in% c("O", "I")], 1:9), 4),
sample(LETTERS[!LETTERS %in% c("O", "I")], 1)), collapse = ''))

myletters <- LETTERS[!LETTERS %in% c("O", "I")]
mydigits <- 1:9
get_key <- function(l, d){

paste0(sample(l, 1), sample(c(l, d), 2, replace = TRUE), collapse = "")

}

replicate(100, get_key(myletters, mydigits))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.