Skip to content

Random Sub-Module Documentation⚓︎


Overview⚓︎

The Random sub-library is a collection of functions structured and designed to generate random strings.

Filtering Results

It is strongely recommended to filter any outputs from the library’s Random and GenerateKey functions as they can return a string that contains inappropriate words. For additional info: here or here.

Functions⚓︎

Random⚓︎

Purpose⚓︎

Generates a random string of a specified length and an optional character set.

The function uses the math.random function to generate random characters from the specified character set, and returns the generated string as the final result.
The function also caches the characters from a specific character set for faster generation in future calls.

Syntax⚓︎

Random(Length: number?, CharSet: string?): string

Parameters⚓︎

  • Length: number Optional
    The length of the string to generate.
    If not specified or nil, the default value is 20 characters.
  • CharSet: string Optional
    The character set to use for generating the string which could be a Lua string pattern.
    If not specified or nil, the default value is "[%w]", which represents any alphanumeric character1.

Returns⚓︎

  • string
    The generated random string.

Examples⚓︎

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
local RandomString = String.Random(10, "[%d]")     -- Generate a random string of length 10 with only digits
print(RandomString)

local RandomString2 = String.Random(15)            -- Generate a random string of length 15 with any alphanumeric character
print(RandomString2)

local RandomString3 = String.Random(5, "[%l%u]")   -- Generate a random string of length 5 with only lowercase and uppercase letters (equivalent to "%a")
print(RandomString3)

local RandomString4 = String.Random(12, "[Aabcd01]")   -- Generate a random string of length 12 that only contains any of characters: A,a,b,c,d,0,1
print(RandomString4)
1
2
3
4
2785220038
4q3m8z9k1p7c6t5
FjKpZ
10AbbbAdb1db

RandomFromString⚓︎

Purpose⚓︎

Used to create a randomized string based on an input one's characters.

Syntax⚓︎

RandomFromString(Str: string, Length: number?, SameCharacterChances: boolean?): string

Parameters⚓︎

  • Str: string
    The input string.
  • Length: number Optional
    The number of characters of the output random string.
    Default: 10
  • SameCharacterChances: boolean Optional
    A boolean determines whether the function should not allow for the repeated characters (higher chances) to exist based on that, but to make the chance of every character to appear the same.
    Default: true.

Returns⚓︎

  • string
    The output random string.

Examples⚓︎

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
local Str = "A string"
print(String.RandomFromString(Str))

local Str = "Hello World!"
print(String.RandomFromString(Str, 10))

local Str = "This string for testing"
print(String.RandomFromString(Str, 15, false))

local Str = "-StringPlus-"
print(String.RandomFromString(Str, 6))

local Str = "123abc"
print(String.RandomFromString(Str, 18))
1
2
3
4
5
stsAnAtnrg
Weedld !W 
hTTgshTneesrhtf
ilrn-l
3aac1a11ba32112a2a

Generate⚓︎

Purpose⚓︎

Generates a randomized string key with a specified number of sections, each with a specified length. It takes in a table which can contain several options.

Syntax⚓︎

GenerateKey(Options: {CharSet: (string | {string})?, TotalSections: number?, SectionLength: number?, Delimiter: string?, Prefix: string?, Suffix: string?}): string

Parameters⚓︎

  • Options: table Optional
    A table of options that specifies how would the returned string is structured.
    These options are:
    • CharSet: (string | {string}) Optional
      The character set to use for generating the string which could be a Lua string pattern.
      If not specified or nil, the default value is "[%w]", which represents any alphanumeric character1.
      • If a table is provided, each element of the table corresponds to a section of the key and specifies the character set for that section.
      • If a single string is provided, all sections of the key will use the same character set.
      • If the number of sets in the table is less than the TotalSections, they will be repeated until the table length matches the TotalSections.
        Also, if CharSet is not provided or is nil, all sections of the key will use any alphanumeric character as the character set.
    • TotalSections: number Optional
      The total number of sections in the returned random string.
      Default value is 4.
    • SectionLength: number Optional
      The length of each section in the key.
      Default: 4.
    • Delimiter: string Optional
      The delimiter used to separate the sections.
      Default: "-".
    • Prefix: string Optional
      The prefix to use for the key.
      Default: nil

      If the prefix length is less than the specified section length, it will be padded with random characters from the character set.
      Also, if the prefix length is greater than the section length, it will be truncated.
    • Suffix: string Optional
      The suffix to use for the key.
      Default: nil

      If the suffix length is less than the specified section length, it will be padded with random characters from the character set.
      If the suffix length is greater than the section length, it will be truncated.

Returns⚓︎

  • string
    The generated random string with the applied options.
  • table
    The orignally generated sections of the returned string in an array.

Examples⚓︎

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
-- Generate a random string whith default settings
local Generated, Sections = String.Generate()
print(Generated, Sections)

-- Generate a random license plate string where it has to be two sections with the length of three characters
-- where also, the first section should only consist upper case characters and the second one only digits
local Generated, Sections = String.Generate({
    CharSet = {[1] = "%u", [2] = "%d"}, 
    SectionLength = 3,
    TotalSections = 2,
})
print(Generated, Sections)

-- Generate a key with 5 sections, each of length 8, using any alphanumeric character as the character set, with a prefix and suffix
local Generated, Sections = String.Generate({
    TotalSections = 5,
    SectionLength = 8,
    Prefix = "PREFIX",
    Suffix = "SUFFIX"
})
print(Generated, Sections)

-- Generate a random phone number string with 4 sections, each of length 4, using any numeric character as the character set, and with a prefix
local Generated, Sections = String.Generate({
    SectionLength = 3,
    Delimiter = " ",
    CharSet = "%d", 
    Prefix = "+15",
})
print(Generated, Sections)

-- Generate a random string with 3 sections of 4 characters each, using a different character set using pattern classes for each section
local Generated = String.Generate({
    CharSet = {"[%d]", "[%l]", "[%u]"},
    TotalSections = 3,
    SectionLength = 4
})
print(Generated)

-- Generate a random string of binary representation with 6 sections of 8 characters each, using a character set consisting of zeros and ones for each section and with a delimiter space character
local Generated = String.Generate({
    Delimiter = " ",
    CharSet = "[01]",
    TotalSections = 6,
    SectionLength = 8
})
print(Generated)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
vdbx-X4Jq-mbDQ-XwRZ {
                [1] = "vdbx",
                [2] = "X4Jq",
                [3] = "mbDQ",
                [4] = "XwRZ"
             }
STE-526 {
           [1] = "STE",
           [2] = "526"
        }
PREFIXcD-kQo67EgZ-ZOQjfP67-eM4RziCb-u8SUFFIX {
                [1] = "Mccx76cD",
                [2] = "kQo67EgZ",
                [3] = "ZOQjfP67",
                [4] = "eM4RziCb",
                [5] = "NLBhAAXN"
             }
+15 757 229 515 {
                [1] = "145",
                [2] = "757",
                [3] = "229",
                [4] = "515"
             }
1148-zbms-CKTK
01110101 00000000 11000110 10111010 10110111 11011100
A Second Return?! Why?

The main purpose of the second return, also known as the generated sections is to make the function flexible and easily interactable with its output string.
An example for its usage, you can filter a specific alphabetic section, such as the text portion of a generated license plate to display to others and to prevent the display of inappropriate content.


  1. Alphanumeric characters are: a-z, A-Z, 0-9