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

@joncuster/randomly

v1.0.0

Published

Optionally deterministic random number utilities for games or other practical randomization needs

Downloads

1

Readme

Randomly

Randomly is a set of optionally deterministic random number utilities for games and other practical randomization needs.

Deterministic behavior is often useful in these settings, not only for testing but e.g. to prevent "save scumming" or for sharing procedural generation seeds.

RNG

RNG is the core module. Initialize with:

import { RNG } from 'random-ly';

const rng = new RNG(seed);
rng.random(); // seedrandom rng
rng.getIntBetween(1,100);

Working with arrays

Get a random item or items from an array (non destructively):

rng.pickFrom(['dog','cat','moose']);
rng.pickNFrom(['dog','cat','moose'], 2);

Shuffle an array (non destructive, returns the shuffled array):

rng.shuffle(['dog','cat','moose']);

Get a random item with a weighted distribution where each item is an object with a property representing its weight (defaults to weight).

rng.pickFromWeighted([{ animal: 'dog', weight: 10 }, { animal: 'cat', weight: 10 }, { animal: 'ferret', weight: 1 } ]);
rng.pickFromWeighted([{ animal: 'dog', chance: 10 }, { animal: 'cat', chance: 10 }, { animal: 'ferret', chance: 1 } ], 'chance');

You can also pass a function as the second argument that takes the item and returns a weight, in this case items need not be objects.

rng.pickFromWeighted(['dog','moose','capybara'], (item) => item.length);

Die

Simulates an n-sided die.

const d8 = new Die({ sides: 8, initialValue: 4 });
d8.value; // 4
d8.roll() // returns rolled value
d8.value; // stores the last-rolled value

DicePool

Simulates a pool of x n-sided dice.

const pool4d12 = new DicePool({ sides: 12, number: 4 });
pool4d12.roll(); // returns the total value of all rolled dice
pool4d12.dice[0].value; // to get the value of an individual die
pool4d12.dice[3].roll(); // (re)roll a single die in the pool

Deck

Simulates a deck of cards (which can be anything, e.g. objects).

const deck = new Deck({ items: [ 'dog', 'cat', 'mouse', 'lizard' ]});
deck.shuffle();
deck.draw(); // draw the top card (it is removed from the deck)
deck.drawHand(3); // draw the top 3 cards (they are removed from the deck)
deck.shuffleInItem('monkey', 2); // shuffle an item randomly into the deck, second arg ensures it is at least x cards down
deck.shuffleInItems(['fish', 'hamster'], 2); // shuffle in multiple items
deck.items; // is just an array with all normal methods available

Randomly

The default import provides all the same functionality as RNG, plus some factory methods for producing die, decks, etc. with the same underlying RNG. This makes it easier to keep everything deterministic.

import Randomly from 'randomly';
Randomly.getIntBetween(1,5); // works like RNG
Randomly.createDie({ sides: 5 }); // returns new Die with same RNG
Randomly.createDicePool({ sides: 6, number: 4 }); // returns new DicePool
Randomly.createDeck({ items: ['foo', 'bar']}); // returns new Deck