Wolfram Research

Function Repository Resource:

RandomString

Source Notebook

Generate a random string

Contributed by: Sander Huisman

ResourceFunction["RandomString"][n]

gives a pseudorandom string of length n using the alphabet of the default language.

ResourceFunction["RandomString"][{n1,n2,,nm}]

gives an n1×n2×…nm-1 array of strings each of length nm using the alphabet of the default language.

ResourceFunction["RandomString"][{s1,s2,},n]

gives a pseudorandom string of length n using the characters si.

ResourceFunction["RandomString"][{w1,w2,}{s1,s2,},n]

gives a pseudorandom string of length n using the characters si weighted using wi.

ResourceFunction["RandomString"][entity,n]

gives a pseudorandom string of length n using the alphabet retrieved from the language or alphabet entity entity.

ResourceFunction["RandomString"]["preset",n]

gives a pseudorandom string of length n using the character preset "preset".

Details and Options

n has to be a non-negative integer or a list of such numbers.
ResourceFunction["RandomString"][] produces a random string of length 1.
The default alphabet is inferred from the global setting $Language.
The weights wi should be non-negative.
Possible forms for the preset "preset" are:
"LowerCase" lowercase letters of the default alphabet
"UpperCase" uppercase letters of the default alphabet
"LowerUpperCase" lower- and uppercase letters of the default alphabet
"Numbers" use the string representation of the numbers 0-9
"LowerCaseNumbers" lowercase letters of the default alphabet and numbers
"UpperCaseNumbers" uppercase letters of the default alphabet and numbers
"LowerUpperCaseNumbers" lower- and uppercase letters of the default alphabet and numbers

Examples

Basic Examples

Create a random string of length 8:

In[1]:=
ResourceFunction["RandomString"][8]
Out[1]=

Create multiple random strings simultaneously:

In[2]:=
ResourceFunction["RandomString"][{3, 8}]
Out[2]=

Randomly choose from a set of characters:

In[3]:=
ResourceFunction["RandomString"][{"a", "d", "e", "n", "r", "s"}, 10]
Out[3]=

Scope


Randomly choose from a set of characters using weights:

In[4]:=
ResourceFunction[
 "RandomString"][{1, 1, 1, 10, 1, 1} -> {"a", "d", "e", "n", "r", "s"}, 20]
Out[4]=

Use an alphabet entity:

In[5]:=
ResourceFunction["RandomString"][
 Entity["Alphabet", "Russian::9qdvh"], 10]
Out[5]=

Use a language entity:

In[6]:=
ResourceFunction["RandomString"][Entity["Language", "Greek"], 10]
Out[6]=

Use a preset of characters:

In[7]:=
ResourceFunction["RandomString"]["UpperCase", 20]
Out[7]=

Generate an array of strings of length 4:

In[8]:=
r = ResourceFunction["RandomString"][{5, 3, 2, 4}]
Out[8]=

The dimensions of the output are Most[dimensions]:

In[9]:=
Dimensions[r] === Most[{5, 3, 2, 4}]
Out[9]=

Arbitrary characters can be used:

In[10]:=
ResourceFunction["RandomString"][CharacterRange[1000, 1020], 100]
Out[10]=

Applications

Create a random password:

In[11]:=
StringRiffle[
 ResourceFunction["RandomString"][
  "LowerUpperCaseNumbers", {4, 5}], "-"]
Out[11]=

Create a random password excluding commonly mistyped characters (0, o and O, etc.):

In[12]:=
StringRiffle[
 ResourceFunction[
  "RandomString"][{"a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "2", "3", "4", "5", "6", "7", "8", "9"}, {4, 5}], "-"]
Out[12]=

Create a random bogus sentence:

In[13]:=
Capitalize[
 StringRiffle[
  ResourceFunction["RandomString"] /@ RandomChoice[{1, 2, 2, 2, 1} -> {3, 4, 5, 6, 7}, 20], {"", " ", "."}]]
Out[13]=

Properties and Relations

Alphabet or language entities can be given:

In[14]:=
SeedRandom[1]; ResourceFunction["RandomString"][
 Entity["Language", "Dutch"], 100]
Out[14]=
In[15]:=
SeedRandom[1]; ResourceFunction["RandomString"][
 Entity["Alphabet", "Dutch::2qc4s"], 100]
Out[15]=

Possible Issues

Some languages have multicharacter letters in their alphabet, such that the supplied length might differ from the result:

In[16]:=
StringLength[
 ResourceFunction["RandomString"][Entity["Language", "Dutch"], 100]]
Out[16]=

If the input contains multicharacter letters, the length might be different than expected:

In[17]:=
StringLength[
 ResourceFunction["RandomString"][{"a", "b", "c", "de"}, 20]]
Out[17]=

RandomString by default uses $Language to determine the alphabet. One can temporarily override it:

In[18]:=
Block[{$Language = "Arabic"},
 ResourceFunction["RandomString"]["UpperLowerCase", 100]
 ]
Out[18]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

See Also

License Information