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

implausible

v3.1.1

Published

Generate pseudorandom numbers and sample probability distributions with optional seed and choice of algorithm.

Downloads

253

Readme

implausible

npm current version license npm dependencies npm dev dependencies node version compatibility snyk advisor npm bundle size (minified) npm bundle size (minified + gzip) gha codeql gha ci gha snyk

implausible is a collection of pseudorandom number generators (PRNGs) and utilities powered by seedrandom.

Quick start

RunKit

RunKit provides one of the most direct ways to get started:

CodePen

Declare imports and global binding in the JS section to get started:

import { prng } from 'https://unpkg.com/implausible@latest?module';
window.prng = prng;

Run commands in the Console section:

prng();
// example output: 0.3722007770466942

Browsers

Insert the following element within the <head> tag of an HTML document:

<script src="https://unpkg.com/implausible@latest"></script>

After the script is loaded, the implausible browser global is exposed:

implausible.prng();
// example output: 0.6736552471595748

Node.js

With npm installed, run terminal command:

npm i implausible

Once installed, declare method imports at the top of each JavaScript file they will be used.

ES2015

Recommended

import {
  prng,
  sample,
  samples,
} from 'implausible';

CommonJS

const {
  prng,
  sample,
  samples,
} = require('implausible');

Usage

Generate stochastic number

prng();
// example output: 0.4471833625387327

prng();
// example output: 0.18700348375416123

...with a specific algorithm

Refer to the list of PRNG names for valid parameter { name } values.

prng({ name: 'xor4096' });
// example output: 0.7105067998636514

Generate deterministic number

prng({ seed: 'hello.' });
// output: 0.9282578795792454

prng({ seed: 'hello.' });
// output: 0.9282578795792454

...with a specific algorithm

Refer to the list of PRNG names for valid parameter { name } values.

prng({
  name: 'xor4096',
  seed: 'hello.',
});
// output: 0.9798525865189731

Stochastic uniform sample

sample({
  collection: [
    'heads',
    'tails',
  ],
});
// example output: 'tails'

...with a specific algorithm

Refer to the list of PRNG names for valid parameter { name } values.

sample({
  collection: [
    'heads',
    'tails',
  ],
  name: 'alea',
});
// example output: 'heads'

Stochastic weighted sample

sample({
  collection: {
    'A-': 6.3,
    'A+': 35.7,
    'AB-': 0.6,
    'AB+': 3.4,
    'B-': 1.5,
    'B+': 8.5,
    'O-': 6.6,
    'O+': 37.4,
  },
});
// example output: 'A+'

...with a specific algorithm

Refer to the list of PRNG names for valid parameter { name } values.

sample({
  collection: {
    'A-': 6.3,
    'A+': 35.7,
    'AB-': 0.6,
    'AB+': 3.4,
    'B-': 1.5,
    'B+': 8.5,
    'O-': 6.6,
    'O+': 37.4,
  },
  name: 'alea',
});
// example output: 'O+'

Deterministic uniform sample

sample({
  collection: [
    'heads',
    'tails',
  ],
  seed: 'hello.',
});
// output: 'tails'

...with a specific algorithm

Refer to the list of PRNG names for valid parameter { name } values.

sample({
  collection: [
    'heads',
    'tails',
  ],
  name: 'tychei',
  seed: 'hello.',
});
// output: 'heads'

Deterministic weighted sample

sample({
  collection: {
    'A-': 6.3,
    'A+': 35.7,
    'AB-': 0.6,
    'AB+': 3.4,
    'B-': 1.5,
    'B+': 8.5,
    'O-': 6.6,
    'O+': 37.4,
  },
  seed: 'hello.',
});
// output: 'A-'

...with a specific algorithm

Refer to the list of PRNG names for valid parameter { name } values.

sample({
  collection: {
    'A-': 6.3,
    'A+': 35.7,
    'AB-': 0.6,
    'AB+': 3.4,
    'B-': 1.5,
    'B+': 8.5,
    'O-': 6.6,
    'O+': 37.4,
  },
  name: 'tychei',
  seed: 'hello.',
});
// output: 'A+'

API

List of PRNG names

The following names of pseudorandom number generators (PRNGs) are valid String inputs for the optional { name } parameter:

  • alea
  • arc4 (default)
  • tychei
  • xor128
  • xor4096
  • xorshift7
  • xorwow

All undefined seed are automatically generated by arc4 before being piped to other generators in stochastic mode. Visit seedrandom documentation for comparative statistics on period and performance.

prng() || prng({ [name][, seed] })

Input

All parameters are optional properties of an optional Object.

| parameter | input type(s) | default | description | | --------- | ------------- | ------- | ----------- | | name | String | arc4 | Refer to the list of PRNG names for values. | | seed | Number, String | undefined (stochastic) | Deterministic when provided, or stochastic when undefined. |

Output

Generates a Number within range: [0, 1) (including 0 and excluding 1).

sample({ collection[, name][, seed] })

See also: samples

Input

All parameters are properties of an Object.

| parameter | input type(s) | default | description | | --------- | ------------- | ------- | ----------- | | collection (required) | Array or Object of {String:Number} pairs | | Array: collection of outcomes with uniform (equally likely) probability distribution (i.e.: coin, dice). Object: histogram where the relative probability of sampling a key is determined by its Number value. | | name | String | arc4 | Refer to the list of PRNG names for values. | | seed | Number, String | undefined (stochastic) | Deterministic when provided, or stochastic when undefined. |

Output

Generates a String weighted random sample from a collection member or key.

samples({ collection[, count][, name][, seed] })

See also: sample

Input

All parameters are properties of an Object.

| parameter | input type(s) | default | description | | --------- | ------------- | ------- | ----------- | | collection (required) | Array or Object of {String:Number} pairs | | Array: collection of outcomes with uniform (equally likely) probability distribution (i.e.: coin, dice). Object: histogram where the relative probability of sampling a key is determined by its Number value. | | count | Number | 1 | Sample size that determines output Array length. | | name | String | arc4 | Refer to the list of PRNG names for values. | | seed | Number, String | undefined (stochastic) | Deterministic when provided, or stochastic when undefined. |

Output

Generates an Array of weighted random sample String from a collection member or key, similar to calling sample multiple times.

Credits

Thanks to David Bau and additional authors for distributing parent package seedrandom under the MIT license.