Random value generators (float, int, vector and noise) for PEX.
npm install pex-random








Random value generators (float, int, vector and noise) for PEX.
``bash`
npm install pex-random
`js
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:
- noise2/3/4: coordinates must be between -2^31 and 2^31. Eg. using Date.now() is not viable but performance.now() is.
- similar seeds might not result in different starting values
module:pex-math~vec2module:pex-math~vec3module:pex-math~quatSummary: Export a Random instance using the global PRNG:
- The instance is seeded by performance.now()random.seed("seed")
- Call to overwrite the global PRNG: all other calls to random.float() will derive from the new seeded state.random.create()
- Call to create a local instance of Random with a separate unpredictable PRNG.random.create("seed")
- Call 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.
Kind: global class
- pex-random
- [new Random([seed])](#new_pex-random_new)
- .create ⇒ Random
- .seed(s)
- [.float([min], [max])](#pex-random+float) ⇒ number
- [.int([min], [max])](#pex-random+int) ⇒ number
- [.vec2([r])](#pex-random+vec2) ⇒ vec2
- [.vec3([r])](#pex-random+vec3) ⇒ vec3
- .vec2InRect(rect) ⇒ vec2
- .vec3InAABB(bbox) ⇒ vec3
- .quat() ⇒ quat
- [.chance([probability])](#pex-random+chance) ⇒ boolean
- .element(list) ⇒ \*
- .noise2(x, y) ⇒ number
- .noise3(x, y, z) ⇒ number
- .noise4(x, y, z, w) ⇒ number
- .fbm(options, ...d) ⇒ number
Creates an instance of Random.
| Param | Type | Default |
| ------ | ------------------------------------------ | ----------------------------------------------------------- |
| [seed] | string \| number | "Random.NOW + Random.#instanceCount" |
Create an instance of Random.
Kind: instance property of pex-random
| Param | Type | Description |
| ------ | ------------------------------------------ | ---------------------------------------------------------------------------------- |
| [seed] | string \| number | If omitted, the global PRNG seed will be used and incremented for each local PRNG. |
Set the seed for the random number generator.
Kind: instance method of pex-random
| Param | Type | Description |
| ----- | ------------------- | ----------- |
| s | string | Seed value |
Get a float between min and max. Defaults to:
- 0 <= x < 1 if no argument supplied0 <= x < max
- if only one argument supplied
Kind: instance method of pex-random
| Param | Type |
| ----- | ------------------- |
| [min] | number |
| [max] | number |
Get an int between min and max. Defaults to:
- 0 <= x < Number.MAX_SAFE_INTEGER if no argument supplied0 <= x < max` if only one argument supplied
-
Kind: instance method of pex-random
| Param | Type |
| ----- | ------------------- |
| [min] | number |
| [max] | number |
Get a vec2 included in a radius.
Kind: instance method of pex-random
| Param | Type | Default | Description |
| ----- | ------------------- | -------------- | ----------- |
| [r] | number | 1 | radius |
Get a vec3 included in a radius.
Kind: instance method of pex-random
| Param | Type | Default | Description |
| ----- | ------------------- | -------------- | ----------- |
| [r] | number | 1 | radius |
Get a vec2 included in a rectangle.
Kind: instance method of pex-random
| Param | Type | Description |
| ----- | ------------------- | ----------- |
| rect | number | rectangle |
Get a vec3 included in a rectangle bbox.
Kind: instance method of pex-random
| Param | Type | Description |
| ----- | ------------------- | -------------- |
| bbox | number | rectangle bbox |
Get a random quaternion.
Kind: instance method of pex-random
See
- [Graphics Gems III, Edited by David Kirk, III.6 UNIFORM RANDOM ROTATIONS]
- Steve LaValle
Returns a chance of an event occuring according to a given probability between 0 and 1.
Kind: instance method of pex-random
| Param | Type | Default | Description |
| ------------- | ------------------- | ---------------- | ---------------------- |
| [probability] | number | 0.5 | Float between 0 and 1. |
Gets a random element from a list.
Kind: instance method of pex-random
| Param | Type |
| ----- | ------------------ |
| list | Array |
Samples the noise field in 2 dimensions.
Kind: instance method of pex-random
Returns: number - in the interval [-1, 1]
| Param | Type |
| ----- | ------------------- |
| x | number |
| y | number |
Samples the noise field in 3 dimensions.
Kind: instance method of pex-random
Returns: number - in the interval [-1, 1]
| Param | Type |
| ----- | ------------------- |
| x | number |
| y | number |
| z | number |
Samples the noise field in 4 dimensions.
Kind: instance method of pex-random
Returns: number - in the interval [-1, 1]
| Param | Type |
| ----- | ------------------- |
| x | number |
| y | number |
| z | number |
| w | number |
Fractional Brownian motion (also called fractal Brownian motion) noise. Default to 1/f noise with 8 octaves.
Kind: instance method of pex-random
Returns: number - in the interval [-1, 1]
| Param | Type | Description |
| ------- | -------------------------------------- | ------------ |
| options | FBMOptions | |
| ...d | number | x, y, z?, w? |
Kind: global typedef
Properties
| Name | Type | Default |
| ------------ | --------------------- | ----------------- |
| [octaves] | number | 8 |
| [lacunarity] | number | 2 |
| [gain] | number | 0.5 |
| [frequency] | number | 1 |
| [amplitude] | number | gain |
| [noise] | function | |
module:pex-math~vec2module:pex-math~vec3module:pex-math~quatKind: global typedef
MIT. See license file.