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

probable

v2.1.4

Published

Utilities for creating and using probability tables.

Downloads

67

Readme

probable

probable is a set of utilities for selecting things at random, including setting up D&D-like random tables.

Here is an example.

If you want to check out the level of randomness of probable, you can have it roll some dice.

Usage

In the browser:

<script src="node_modules/probable/probable.js"></script>

In Node:

var probable = require('probable');

From there, you just use the probable object.

roll is a function that basically rolls a die for you. It will randomly generate an integer between 0 and one less than the number of "sides" you specify. For example, probable.roll(6) will return 0, 1, 2, 3, 4, or 5.

rollDie is like roll, except 1-based, like traditional dice. probable.roll(6) will return 1, 2, 3, 4, 5, or 6.

pickFromArray will randomly pick an element of an array that you give it, giving each element equal weight.

createRangeTable will create an object for you that represents a table that maps ranges of numbers to outcomes. If you give it

[
  [[0, 80], 'a'],
  [[81, 95], 'b'],
  [[96, 99], 'c']
]

— it will create a table in which indexes 0 through 80 map to an outcome of 'a', 81-95 map to an outcome of 'b', and 96-99 map to an outcome of 'c'. You can use its outcomeAtIndex to get the outcome for a particular index.

You can use the roll method to randomly select an index covered by the definition (in the example above, 0-99), then return the outcome corresponding to it.

createRangeTableFromDict is a wrapper for createRangeTable that may be more convenient in some cases. Instead of giving it explicit ranges, you just specify how much probabiity space you want each outcome to take up. The createRangeFromTable above would be created by createRangeTableFromDict if you gave it this dictionary:

{
  a: 80,
  b: 15,
  c: 5
}

The drawback of this method is that all of the outcomes must be strings.

createTableFromDef is another wrapper for createRangeTable that takes a table definition that looks like this:

{
  '0': 'Nothing',
  '1-2': 'Poison gas',
  '3': 'Ruby',
  '4': 'Bedbugs',
  '5-7': 'Bag of gold'
}

Where the ranges are the keys of the object (e.g. '1-2') and the values are the outcomes. So, in this case, probable will roll an 8-sided die (with "faces" 0-7) and if the outcome is a 1-2, the outcome is 'Poison gas'. If it is a 4, the outcome is 'Bedbugs'.

These tables can cascade like so:

{
  '0-20': 'a',
  '21-95': {
    '0-39': {
      '0-24': 'Bulbasaur',
      '25-66': 'Squirtle',
      '67-99': 'Charmander'
    },
    '40-55': [
      'Human',
      'Dwarf',
      'Elf',
      'Illithid'
    ],
    '56-99': 'Rock'
  },
  '96-100': 'c'
};

Here, if a 25 is rolled, probable then will roll on the subtable defined in the outcome. If it rolls a 70, the outcome will be 'Rock'. If it rolls a 35 on the subtable, the outcome is yet another subtable, so it will roll again. Let's say it rolls a 50 for that. The ultimate outcome will then be 'Squirtle.'

Another – possibly more convenient – alternative to createTableFromDef is wrapper for createTableFromSizes that takes a table definition that uses probabilty size instead of fixed ranges:

[
  [1, 'Nothing'],
  [2, 'Poison gas'],
  [1, 'Ruby'],
  [1, 'Bedbugs'],
  [5, 'Bag of gold']
]

Here, 'Bag of gold' is an outcome that's five times as likely as 'Nothing', 'Ruby', and 'Bedbugs'. This way of defining tables has the advantage that changing the likelihood of one outcome does not mean that have to update ranges for every outcome that follows it. e.g. If we change our minds and decide that 'Ruby' should be three times as likely as 'Nothing', we only have to change the entry for 'Ruby', like so:

[
  [1, 'Nothing'],
  [2, 'Poison gas'],
  [3, 'Ruby'],
  [1, 'Bedbugs'],
  [5, 'Bag of gold']
]

If we were using createTableFromDef, we'd have to adjust the ranges for 'Bedbugs' and 'Bag of gold' as well.

There is a limitation to createTableFromSizes, however. If an outcome is an array of pairs, and the first element of the pair is number, it will interpret that as nested subtable definition and act accordingly. e.g.

[
  [1, 'Nothing'],
  [2, 'Poison gas'],
  [3, 'Ruby'],
  [1, 'Bedbugs'],
  [
    5,
    [
      [999, 'Bag of gold'],
      [1, 'One million bags of gold']
    ]
  ]
]

Here, if the outer roll is a 9, the outcome will not be:

[
  [999, 'Bag of gold'],
  [1, 'Bag of God']
]

Instead, it will do another roll on what it thinks is a subtable, and the outcome will either be 'Bag of gold' or 'Bag of God'. (Probably just 'Bag of gold', though.)

createProbable is a function that lets you create another instance of probable that uses a random function other than Math.random, such as something constructed with seedrandom. Any function that returns a value between 0 and 1 works as the parameter for this function.

shuffle and sample are like the shuffle and sample from Underscore, except that it uses whatever random function you provided to createProbable instead of Math.random.

Installation

npm install probable

Tests

make test

Run probable.sample tests (~11 seconds on a 2015 MBP) with:

node tests/sample-tests.js

Lint:

make lint

License

ISC.