samedi 11 mars 2017

Generating Random Strings in R

I want to generate random strings in the following way

ABCDE1234E i.e each string contains 5 Characters, 4 Numerics, 1 Char again.

I figured out a way to create this using the following code.

library(random)
string_5 <- as.vector(randomStrings(n=5000, len=5, digits=FALSE, upperalpha=TRUE,
                        loweralpha=FALSE, unique=TRUE, check=TRUE))
number_4 <- as.vector(randomNumbers(n=5000, min=1111, max=9999, col=5, base=10, check=TRUE))
string_1 <- as.vector(randomStrings(n=5000, len=1, digits=FALSE, upperalpha=TRUE,
                         loweralpha=FALSE, unique=FALSE, check=TRUE))
PAN.Number <- paste(string_5,number_4,string_1,sep = "")

But these functions are taking longer time and random library needs network connection.

> system.time(string_5 <- as.vector(randomStrings(n=5000, len=5, digits=FALSE, upperalpha=TRUE,
+                                                 loweralpha=FALSE, unique=TRUE, check=TRUE)))
   user  system elapsed 
   0.07    0.00    3.18 

Is there any method that i could try to reduce the execution time. I also tried using sample() but i couldn't figure it out. Thank You !

Aucun commentaire:

Enregistrer un commentaire