Random

Generates random values of different kinds. Some methods are deprecated and have been moved to dedicated modules.

Alpha

Generating a string consisting of lower/upper alpha characters based on count and upcase options.

Parameters

NameTypeDefaultDescription
optionsnumber | { ... }{}

Either the number of characters or an options instance.

options.bannedChars?readonly string[][]

An array with characters to exclude.

options.count?number1

The number of characters to generate.

options.upcase?booleanfalse

If true, the result will be uppercase. If false, it will be lowercase.

Returns: string

faker.random.alpha(options: number | {
  bannedChars: readonly string[],
  count: number,
  upcase: boolean
} = {}): string
faker.random.alpha() // => "o"
faker.random.alpha() // 'b'
faker.random.alpha(10) // 'qccrabobaf'
faker.random.alpha({ count: 5, upcase: true, bannedChars: ['a'] }) // 'DTCIC'

Alpha Numeric

Generating a string consisting of lower/upper alpha characters and digits based on count and upcase options.

Parameters

NameTypeDefaultDescription
countnumber1

The number of characters and digits to generate.

options{ ... }{}

The options to use.

options.bannedChars?readonly string[][]

An array of characters and digits which should be banned in the generated string.

Returns: string

faker.random.alphaNumeric(count: number = 1, options: {
  bannedChars: readonly string[]
} = {}): string
faker.random.alphaNumeric() // => "j"
faker.random.alphaNumeric() // '2'
faker.random.alphaNumeric(5) // '3e5v7'
faker.random.alphaNumeric(5, { bannedChars: ["a"] }) // 'xszlm'

Array Element

Deprecated

This method is deprecated and will be removed in a future version.

Returns random element from the given array.

Parameters

NameTypeDefaultDescription
<T>

The type of the entries to pick from.

arrayreadonly T[]['a', 'b', 'c']

Array to pick the value from.

Returns: T

faker.random.arrayElement<T>(array: readonly T[] = ['a', 'b', 'c']): T
faker.random.arrayElement() // => "b"
faker.random.arrayElement() // 'b'
faker.random.arrayElement(['cat', 'dog', 'mouse']) // 'dog'

Array Elements

Deprecated

This method is deprecated and will be removed in a future version.

Returns a subset with random elements of the given array in random order.

Parameters

NameTypeDefaultDescription
<T>

The type of the entries to pick from.

arrayreadonly T[]['a', 'b', 'c']

Array to pick the value from.

count?number

Number of elements to pick. When not provided, random number of elements will be picked. When value exceeds array boundaries, it will be limited to stay inside.

Returns: T[]

faker.random.arrayElements<T>(array: readonly T[] = ['a', 'b', 'c'], count?: number): T[]
faker.random.arrayElements() // => ["c","b"]
faker.random.arrayElements() // ['b', 'c']
faker.random.arrayElements(['cat', 'dog', 'mouse']) // ['mouse', 'cat']
faker.random.arrayElements([1, 2, 3, 4, 5], 2) // [4, 2]

Boolean

Deprecated

This method is deprecated and will be removed in a future version.

Returns the boolean value true or false.

Returns: boolean

faker.random.boolean(): boolean
faker.random.boolean() // => true
faker.random.boolean() // false

Float

Deprecated

This method is deprecated and will be removed in a future version.

Returns a single random floating-point number for the given precision or range and precision.

Parameters

NameTypeDefaultDescription
options?number | { ... }

Precision or options object.

options.max?number99999

Upper bound for generated number.

options.min?number0

Lower bound for generated number.

options.precision?number0.01

Precision of the generated number.

Returns: number

faker.random.float(options?: number | {
  max: number,
  min: number,
  precision: number
}): number
faker.random.float() // => 54880.8
faker.random.float() // 51696.36
faker.random.float(0.1) // 52023.2
faker.random.float({ min: 1000000 }) // 212859.76
faker.random.float({ max: 100 }) // 28.11
faker.random.float({ precision: 0.1 }) // 84055.3
faker.random.float({ min: 10, max: 100, precision: 0.001 }) // 57.315

Hexa Decimal

Deprecated

This method is deprecated and will be removed in a future version.

Returns a hexadecimal number.

Parameters

NameTypeDefaultDescription
count?number1

Length of the generated number.

Returns: string

faker.random.hexaDecimal(count?: number = 1): string
faker.random.hexaDecimal() // => "0xc"
faker.random.hexaDecimal() // '0xb'
faker.random.hexaDecimal(10) // '0xaE13F044fb'

Image

Deprecated

This method is deprecated and will be removed in a future version.

Returns a random image url.

Returns: string

faker.random.image(): string
faker.random.image() // => "http://loremflickr.com/640/480/fashion"
faker.random.image() // 'http://placeimg.com/640/480/animals'

Locale

Returns a random locale, that is available in this faker instance. You can use the returned locale with faker.setLocale(result).

Returns: string

faker.random.locale(): string
faker.random.locale() // => "hu"
faker.random.locale() // 'el'

Number

Deprecated

This method is deprecated and will be removed in a future version.

Returns a single random number between zero and the given max value or the given range with the specified precision. The bounds are inclusive.

Parameters

NameTypeDefaultDescription
options?number | { ... }

Maximum value or options object.

options.max?number99999

Upper bound for generated number.

options.min?number0

Lower bound for generated number.

options.precision?number1

Precision of the generated number.

Returns: number

faker.random.number(options?: number | {
  max: number,
  min: number,
  precision: number
}): number
faker.random.number() // => 54881
faker.random.number() // 55422
faker.random.number(100) // 52
faker.random.number({ min: 1000000 }) // 431433
faker.random.number({ max: 100 }) // 42
faker.random.number({ precision: 0.01 }) // 64246.18
faker.random.number({ min: 10, max: 100, precision: 0.01 }) // 36.94

Numeric

Generates a given length string of digits.

Parameters

NameTypeDefaultDescription
lengthnumber1

The number of digits to generate.

options{ ... }{}

The options to use.

options.allowLeadingZeros?booleanfalse

If true, leading zeros will be allowed.

options.bannedDigits?readonly string[][]

An array of digits which should be banned in the generated string.

Returns: string

faker.random.numeric(length: number = 1, options: {
  allowLeadingZeros: boolean,
  bannedDigits: readonly string[]
} = {}): string
faker.random.numeric() // => "5"
faker.random.numeric() // '2'
faker.random.numeric(5) // '31507'
faker.random.numeric(42) // '56434563150765416546479875435481513188548'
faker.random.numeric(42, { allowLeadingZeros: true }) // '00564846278453876543517840713421451546115'
faker.random.numeric(6, { bannedDigits: ['0'] }) // '943228'

Object Element

Deprecated

This method is deprecated and will be removed in a future version.

Returns a random key or value from given object.

Parameters

NameTypeDefaultDescription
<T>Record<string, unknown>

The type of Record to pick from.

<K>number | string | symbol

The keys of T.

object?T{ foo: 'bar', too: 'car' }

The object to get the keys or values from.

field?'key' | 'value''value'

If this is set to 'key', this method will a return a random key of the given instance. If this is set to 'value', this method will a return a random value of the given instance.

Returns: K | T[K]

faker.random.objectElement<T, K>(object?: T = { foo: 'bar', too: 'car' }, field?: 'key' | 'value' = 'value'): K | T[K]
faker.random.objectElement() // => "car"
const object = { keyA: 'valueA', keyB: 42 };
faker.random.objectElement(object) // 42
faker.random.objectElement(object, 'key') // 'keyB'
faker.random.objectElement(object, 'value') // 'valueA'

Uuid

Deprecated

This method is deprecated and will be removed in a future version.

Returns a UUID v4 (Universally Unique Identifier).

Returns: string

faker.random.uuid(): string
faker.random.uuid() // => "89bd9d8d-69a6-474e-8f46-7cc8796ed151"
faker.random.uuid() // '4136cd0b-d90b-4af7-b485-5d1ded8db252'

Word

Returns random word.

Returns: string

faker.random.word(): string
faker.random.word() // => "Montenegro"
faker.random.word() // 'Seamless'

Words

Returns string with set of random words.

Parameters

NameTypeDefaultDescription
count?number

Number of words. Defaults to a random value between 1 and 3.

Returns: string

faker.random.words(count?: number): string
faker.random.words() // => "Account Loan"
faker.random.words() // 'neural'
faker.random.words(5) // 'copy Handcrafted bus client-server Point'