HDK Utilities Library
classes
Randomseed

Class: RandomSeed

This is a util that implements a liner congruential generator to generate random number.

See

https://en.wikipedia.org/wiki/Linear_congruential_generator (opens in a new tab)

Usage

const seed = new RandomSeed(1234);
const value = seed.value();
const range = seed.range(1, 10);
const fromArray = seed.fromArray(['a', 'b', 'c']);
 
console.log(value, range, fromArray);

Table of contents

Constructors

Methods

Constructors

constructor

new RandomSeed(seed?): RandomSeed

Parameters

NameTypeDescription
seed?numberoptional seed to use, if not provided a fixed value will be used.

Returns

RandomSeed

Methods

fromArray

fromArray<T>(array): T

Type parameters

NameDescription
Tthe identity type, given an array of strings for example, the returned random item will be string

Parameters

NameType
arrayT[]

Returns

T

a random item from the given array


int

int(min?, max?): number

Parameters

NameTypeDefault valueDescription
minnumber0min value
maxnumber2147483647max value

Returns

number

  • a random number based on the current seed

reset

reset(): void

resets the current seed to the origin seed

Returns

void