Skip to content

Function: randomString() ​

Call Signature ​

randomString(): string

Generate a random string of 32 letters and digits ([A-Za-z0-9]), drawn from the cryptographic random generator.

Returns ​

string

A new random string.

Remarks ​

Each character is picked uniformly from the character set (the random values which would bias the pick are drawn again), so the strings are fit for tokens and secrets.

Example ​

typescript
import {randomString} from 'lakutata/utils'

console.log(randomString().length, randomString(6, 'numeric').length)
//32 6
console.log(/^[0-9a-f]{16}$/.test(randomString(16, 'hex')))
//true
console.log(/^[0-9XYZ]{10}$/.test(randomString(10, ['numeric', 'XYZ'])))
//true

Call Signature ​

randomString(length): string

Generate a random string of letters and digits ([A-Za-z0-9]) of a given length.

Parameters ​

length ​

number

The number of characters: a non-negative integer (0 gives '').

Returns ​

string

A new random string.

Throws ​

When length is negative or not an integer.

Call Signature ​

randomString(length, charset): string

Generate a random string of a given length from a character set: one of the named sets ('alphanumeric', 'alphabetic', 'numeric', 'hex', 'binary', 'octal'), or any other string, taken as the list of its characters ('ABC123'; a character repeated in it is picked more often).

Parameters ​

length ​

number

The number of characters: a non-negative integer (0 gives '').

charset ​

string

A named set, or the characters to pick from, not empty. The characters are the code points of the string: a character outside the Basic Multilingual Plane (an emoji) is picked whole.

Returns ​

string

A new random string.

Throws ​

When length is negative or not an integer, or when charset is empty.

Call Signature ​

randomString(length, charset): string

Generate a random string of letters and digits ([A-Za-z0-9]) of a given length.

Parameters ​

length ​

number

The number of characters: a non-negative integer.

charset ​

"alphanumeric"

'alphanumeric'.

Returns ​

string

A new random string.

Throws ​

When length is negative or not an integer.

Call Signature ​

randomString(length, charset): string

Generate a random string of letters ([A-Za-z]) of a given length.

Parameters ​

length ​

number

The number of characters: a non-negative integer.

charset ​

"alphabetic"

'alphabetic'.

Returns ​

string

A new random string.

Throws ​

When length is negative or not an integer.

Call Signature ​

randomString(length, charset): string

Generate a random string of decimal digits ([0-9]) of a given length; it may start with 0.

Parameters ​

length ​

number

The number of characters: a non-negative integer.

charset ​

"numeric"

'numeric'.

Returns ​

string

A new random string.

Throws ​

When length is negative or not an integer.

Call Signature ​

randomString(length, charset): string

Generate a random string of lower case hexadecimal digits ([0-9a-f]) of a given length.

Parameters ​

length ​

number

The number of characters: a non-negative integer.

charset ​

"hex"

'hex'.

Returns ​

string

A new random string.

Throws ​

When length is negative or not an integer.

Call Signature ​

randomString(length, charset): string

Generate a random string of binary digits ([01]) of a given length.

Parameters ​

length ​

number

The number of characters: a non-negative integer.

charset ​

"binary"

'binary'.

Returns ​

string

A new random string.

Throws ​

When length is negative or not an integer.

Call Signature ​

randomString(length, charset): string

Generate a random string of octal digits ([0-7]) of a given length.

Parameters ​

length ​

number

The number of characters: a non-negative integer.

charset ​

"octal"

'octal'.

Returns ​

string

A new random string.

Throws ​

When length is negative or not an integer.

Call Signature ​

randomString(length, charset): string

Generate a random string of a given length from several character sets joined: each item is a named set ('numeric'…) or a string of characters (['numeric', 'XYZ'] picks from 0-9XYZ). The bigger sets are picked from more often; no set is guaranteed to appear.

Parameters ​

length ​

number

The number of characters: a non-negative integer.

charset ​

string[]

The named sets and the strings of characters (their code points); their union must not be empty.

Returns ​

string

A new random string.

Throws ​

When length is negative or not an integer, or when the union of the sets is empty.

Released under the MIT License.