npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pex-random

v2.1.2

Published

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

Downloads

67

Readme

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.

pex-random

Kind: global class

new Random([seed])

Creates an instance of Random.

| Param | Type | Default | | ------ | ------------------------------------------ | ----------------------------------------------------------- | | [seed] | string | number | "Random.NOW + Random.#instanceCount" |

pex-random.create ⇒ Random

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. |

pex-random.seed(s)

Set the seed for the random number generator.

Kind: instance method of pex-random

| Param | Type | Description | | ----- | ------------------- | ----------- | | s | string | Seed value |

pex-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 pex-random

| Param | Type | | ----- | ------------------- | | [min] | number | | [max] | number |

pex-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 pex-random

| Param | Type | | ----- | ------------------- | | [min] | number | | [max] | number |

pex-random.vec2([r]) ⇒ vec2

Get a vec2 included in a radius.

Kind: instance method of pex-random

| Param | Type | Default | Description | | ----- | ------------------- | -------------- | ----------- | | [r] | number | 1 | radius |

pex-random.vec3([r]) ⇒ vec3

Get a vec3 included in a radius.

Kind: instance method of pex-random

| Param | Type | Default | Description | | ----- | ------------------- | -------------- | ----------- | | [r] | number | 1 | radius |

pex-random.vec2InRect(rect) ⇒ vec2

Get a vec2 included in a rectangle.

Kind: instance method of pex-random

| Param | Type | Description | | ----- | ------------------- | ----------- | | rect | number | rectangle |

pex-random.vec3InAABB(bbox) ⇒ vec3

Get a vec3 included in a rectangle bbox.

Kind: instance method of pex-random

| Param | Type | Description | | ----- | ------------------- | -------------- | | bbox | number | rectangle bbox |

pex-random.quat() ⇒ quat

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

pex-random.chance([probability]) ⇒ boolean

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. |

pex-random.element(list) ⇒ *

Gets a random element from a list.

Kind: instance method of pex-random

| Param | Type | | ----- | ------------------ | | list | Array |

pex-random.noise2(x, y) ⇒ number

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 |

pex-random.noise3(x, y, z) ⇒ 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 |

pex-random.noise4(x, y, z, w) ⇒ 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 |

pex-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 pex-random Returns: number - in the interval [-1, 1]

| Param | Type | Description | | ------- | -------------------------------------- | ------------ | | options | FBMOptions | | | ...d | number | x, y, z?, w? |

FBMOptions

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 | |

vec2 : module:pex-math~vec2

Kind: global typedef

vec3 : module:pex-math~vec3

Kind: global typedef

quat : module:pex-math~quat

Kind: global typedef

License

MIT. See license file.