I want to generate random strings in the following way: ABCDE1234E
, i.e each string contains 5 Characters, 4 Numerics, then 1 Char.
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 a long time and the random
library needs a 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.
Using "stringi" as suggested by @akrun will be faster, but the following is also very fast and does not require any additional packages:
Example output:
We can use
stri_rand_strings
fromstringi
Or more compactly
Benchmarks
Was able to reproduce the timings even for one part of the expected output using OP's method
You can directly perform what you want: Sample random 5 capital letters Sample 4 digits Sample 1 random capital letter
This will be more easily controlled, and won't take as long.
Your performance problem comes from using the
random
package in the first place: it's understandable that you could find therandom::randomStrings()
function in an internet search and think it's a good way to generate random strings for use in a program, but therandom
package is not intended for general-purpose programming. It works by querying the RANDOM.ORG server, which is intrinsically slower than R's built-in pseudo-random number generators.From one of the vignettes from the random package:
Note that most of these examples are about seeding or initializing (these are synonyms) R's built-in pseudo-random number generators, rather than replacing them ...