Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating Random Strings

Tags:

random

r

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.

like image 556
Nikhil Kumar Avatar asked Mar 11 '17 11:03

Nikhil Kumar


People also ask

How do you generate random strings?

Using the random index number, we have generated the random character from the string alphabet. We then used the StringBuilder class to append all the characters together. If we want to change the random string into lower case, we can use the toLowerCase() method of the String .

What is random string generator?

Random strings can be unique. Used in computing, a random string generator can also be called a random character string generator. This is an important tool if you want to generate a unique set of strings. The utility generates a sequence that lacks a pattern and is random.

How do you generate random numbers and strings?

Example 1: Generate Random Stringsrandom() method is used to generate random characters from the specified characters (A-Z, a-z, 0-9). The for loop is used to loop through the number passed into the generateString() function. During each iteration, a random character is generated.

How do you generate a random string from an alphabet string?

Initialize an empty string and name it as “ran”. Choose the size of the string to be generated. Now using Next () method generate a random number and select the character at that index in the alphabet string. Append that character to randomString. Repeat steps 4 and 5 for n time where n is the length of the string.

How do you generate a random hash string?

Hash Generator. For random MD5 strings, set the base to 16 (hexadecimal) and the length to 32 characters. For random SHA-1 hash strings, set the base to 16 (hexadecimal) and the length to 40 characters.

How many random strings can be generated from a list?

generate-random.org allows you to generate up to 500 unique random strings from 1 characters to 256, with their md5, sha256 and sha512 representation. Our tool makes sure that every string in your list will be unique, and will only be added once.

How to generate random characters from ASCII in Java?

The ASCII value is converted into character using the ToChar () method. This entire step will be repeated multiple times using for loop and a string is formed by appending all the randomly generated characters. We can also generate random strings using this method. In this method, we pass a string that contains 26 alphabets.


2 Answers

Using "stringi" as suggested by @akrun will be faster, but the following is also very fast and does not require any additional packages:

myFun <- function(n = 5000) {   a <- do.call(paste0, replicate(5, sample(LETTERS, n, TRUE), FALSE))   paste0(a, sprintf("%04d", sample(9999, n, TRUE)), sample(LETTERS, n, TRUE)) } 

Example output:

myFun(10) ##  [1] "BZHOF3737P" "EPOWI0674X" "YYWEB2825M" "HQIXJ5187K" "IYIMB2578R" ##  [6] "YSGBG6609I" "OBLBL6409Q" "PUMAL5632D" "ABRAT4481L" "FNVEN7870Q" 
like image 147
A5C1D2H2I1M1N2O1R2T1 Avatar answered Sep 20 '22 05:09

A5C1D2H2I1M1N2O1R2T1


We can use stri_rand_strings from stringi

library(stringi) sprintf("%s%s%s", stri_rand_strings(5, 5, '[A-Z]'),       stri_rand_strings(5, 4, '[0-9]'), stri_rand_strings(5, 1, '[A-Z]')) 

Or more compactly

do.call(paste0, Map(stri_rand_strings, n=5, length=c(5, 4, 1),             pattern = c('[A-Z]', '[0-9]', '[A-Z]'))) 

Benchmarks

system.time({     do.call(paste0, Map(stri_rand_strings, n=5000, length=c(5, 4, 1),             pattern = c('[A-Z]', '[0-9]', '[A-Z]')))     }) #  user  system elapsed  #   0      0      0 

Was able to reproduce the timings even for one part of the expected output using OP's method

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.86    0.24    5.52  
like image 43
akrun Avatar answered Sep 20 '22 05:09

akrun