2.1.0 • Published 15 days ago

pex-random v2.1.0

Weekly downloads
26
License
MIT
Repository
github
Last release
15 days ago

pex-random

npm version stability-stable npm minzipped size dependencies types Conventional Commits styled with prettier linted with eslint license

Random value generators (float, int, vector and noise) for PEX.

Installation

npm install pex-random

Usage

import random from "pex-random";

// Use global PRNG
console.log(random.float());
// => unpredictable (seeded by performance.now())

// Seed global PRNG
random.seed("0");
console.log(random.float());
// => predictable, always returns: 0.8071179636424909

// Use local PRNG
const localRandom = random.create();
console.log(localRandom.float());
// => unpredictable (seeded by performance.now() + INCREMENT)

// Use seeded local PRNG
const localSeededRandom = random.create("0");
console.log(localSeededRandom.float());
// => predictable, always returns: 0.8071179636424909

Notes:

API

Modules

Classes

Typedefs

pex-random

Summary: Export a Random instance using the global PRNG:

  • The instance is seeded by performance.now()
  • Call random.seed("seed") to overwrite the global PRNG: all other calls to random.float() will derive from the new seeded state.
  • Call random.create() to create a local instance of Random with a separate unpredictable PRNG.
  • Call random.create("seed") to create a local instance of Random with a separate predictable PRNG: all other calls to random.float() will derive from the new seeded state.

Random

Kind: global class

new Random(seed)

Creates an instance of Random.

ParamTypeDefault
seedstring | number"Random.NOW + Random.#instanceCount"

random.create ⇒ Random

Create an instance of Random.

Kind: instance property of Random

ParamTypeDescription
seedstring | numberIf omitted, the global PRNG seed will be used and incremented for each local PRNG.

random.seed(s)

Set the seed for the random number generator.

Kind: instance method of Random

ParamTypeDescription
sstringSeed value

random.float(min, max) ⇒ number

Get a float between min and max. Defaults to:

  • 0 <= x < 1 if no argument supplied
  • 0 <= x < max if only one argument supplied

Kind: instance method of Random

ParamType
minnumber
maxnumber

random.int(min, max) ⇒ number

Get an int between min and max. Defaults to:

  • 0 <= x < Number.MAX_SAFE_INTEGER if no argument supplied
  • 0 <= x < max if only one argument supplied

Kind: instance method of Random

ParamType
minnumber
maxnumber

random.vec2(r) ⇒ module:pex-math~vec2

Get a vec2 included in a radius.

Kind: instance method of Random

ParamTypeDefaultDescription
rnumber1radius

random.vec3(r) ⇒ module:pex-math~vec3

Get a vec3 included in a radius.

Kind: instance method of Random

ParamTypeDefaultDescription
rnumber1radius

random.vec2InRect(rect) ⇒ module:pex-math~vec2

Get a vec2 included in a rectangle.

Kind: instance method of Random

ParamTypeDescription
rectnumberrectangle

random.vec3InAABB(bbox) ⇒ module:pex-math~vec3

Get a vec3 included in a rectangle bbox.

Kind: instance method of Random

ParamTypeDescription
bboxnumberrectangle bbox

random.quat() ⇒ module:pex-math~quat

Get a random quaternion.

Kind: instance method of Random See

random.chance(probability) ⇒ boolean

Returns a chance of an event occuring according to a given probability between 0 and 1.

Kind: instance method of Random

ParamTypeDefaultDescription
probabilitynumber0.5Float between 0 and 1.

random.element(list) ⇒ *

Gets a random element from a list.

Kind: instance method of Random

ParamType
listArray

random.noise2(x, y) ⇒ number

Samples the noise field in 2 dimensions.

Kind: instance method of Random Returns: number - in the interval -1, 1

ParamType
xnumber
ynumber

random.noise3(x, y, z) ⇒ number

Samples the noise field in 3 dimensions.

Kind: instance method of Random Returns: number - in the interval -1, 1

ParamType
xnumber
ynumber
znumber

random.noise4(x, y, z, w) ⇒ number

Samples the noise field in 4 dimensions.

Kind: instance method of Random Returns: number - in the interval -1, 1

ParamType
xnumber
ynumber
znumber
wnumber

random.fbm(options, ...d) ⇒ number

Fractional Brownian motion (also called fractal Brownian motion) noise. Default to 1/f noise with 8 octaves.

Kind: instance method of Random Returns: number - in the interval -1, 1

ParamTypeDescription
optionsFBMOptions
...dnumberx, y, z?, w?

FBMOptions

Kind: global typedef Properties

NameTypeDefault
octavesnumber8
lacunaritynumber2
gainnumber0.5
frequencynumber1
amplitudenumbergain
noisefunction

License

MIT. See license file.

2.1.0

15 days ago

2.0.0

3 months ago

2.0.0-alpha.2

8 months ago

2.0.0-alpha.1

2 years ago

2.0.0-alpha.0

2 years ago

1.0.1

7 years ago

1.0.0

8 years ago

1.0.0-beta.1

8 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.1

10 years ago