i need randomly reorder digits of integer x
(at least 4 or more digits). following sort of works not ensure 0 never comes @ beginning of reordered value.
if 0 become initial value, reordered integer have less nchar
trying avoid.
x = 357230434l set.seed(42) as.integer(paste(sample(unlist(strsplit(as.character(x),""))), collapse = "")) #[1] 437324035
i haven't tried yet may able for
loop. however, before attempt that, wanted see if there better way reorder digits of integer while ensuring 0 never placed @ beginning of reordered integer.
have considered doing in multiple parts? there might easier way, 1 way randomly pull first non-zero character , sample rest. like:
chars<-unlist(strsplit(as.character(x),"")) first<-sample(chars[chars!="0"],1) as.integer(paste0(c(first,sample(chars[-match(first,chars)])),collapse=""))
running few different seeds:
set.seed(42) #447323035 set.seed(43) #243057343 set.seed(44) #472333504 set.seed(45) #475433320 set.seed(46) #570244333
Comments
Post a Comment