Random Unique numbers (Whole numbers)

Hey everyone,

I am trying to create a unique ID, which is supposed to be whole numbers, have 5 digits, and be randomly picked.

Researching this, I only came across the generation of random numbers that are either fraction numbers or not unique.

I am also completely new to R, so it might be an easy solution!

I appreciate every help.


(df_1 <- data.frame(unlabelled_stuff=c(1:12)))

# 5 digits numbers are in the range 10,000 - 99,9999 i.e. there are 89999 of them
# They are these : 
  (possibles <- seq(from=10000,to=99999))

#make a function to help us make a vector of arbitrary size that comes from that
assign_from_possible <- function(n,seed=42){
  if(n>89999 | n<0){
    stop("You asked for an impossible quantity meeting the criteria")
  }
    set.seed(seed)
    sample( x=seq(from=10000,to=99999),
            size = n,
            replace = FALSE)
  
}
#use it
df_1$unique_rand_id <- assign_from_possible(nrow(df_1))
# see result
df_1
1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.