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 ornil
, the default value is20
characters.CharSet: string
Optional
The character set to use for generating the string which could be a Lua string pattern.
If not specified ornil
, 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 |
|
1 2 3 4 |
|
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 |
|
1 2 3 4 5 |
|
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 ornil
, 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 theTotalSections
.
Also, if CharSet is not provided or isnil
, 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 is4
.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 |
|
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 |
|
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.