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 🙏

© 2026 – Pkg Stats / Ryan Hefner

shuffle-duplication

v1.1.0

Published

Shuffle duplication using seeds for autochess-like games

Downloads

426

Readme

shuffle-duplication

Advanced shuffle duplication for autochess-likes and other games, as explained in the following article:

Demo

import { createHaystack, randomNeedle } from 'shuffle-duplication';
import { createPcg32, randomInt } from 'pcg';

let seedGenerator = createPcg32({}, 42, 54);

const randomUint32 = () => {
  const ret = randomInt(0, 2 ** 32, seedGenerator);
  seedGenerator = ret[1];
  return ret[0];
};

const randomUint64 = () => {
  const lo = BigInt(randomUint32());
  const hi = BigInt(randomUint32());
  return (hi << 32n) | lo;
};

const CARDS = {
  '2': 'Diana',
  '3': 'Elise',
  '4': 'Fiora',
  '5': 'Garen',
  '6': 'Maokai',
  '7': 'Nidalee',
  '8': 'Tahm Kench',
  '9': 'Twisted Fate',
  '10': 'Wukong',
  '11': 'Yasuo',
};

const seatSeeds = [];
const seatStates = [];
const seatShops = [];
for (let i = 0; i < 8; i++) {
  seatSeeds.push(randomUint64());
  seatStates.push(createHaystack(seatSeeds[i]));
  seatShops.push([]);
}

const pool = {};
for (const id of Object.keys(CARDS)) {
  pool[id] = 29;
}

for (let k = 0; k < 5; k++) {
  for (let i = 0; i < 8; i++) {
    seatShops[i].push(randomNeedle(seatStates[i], pool));
  }
  for (let i = 0; i < 8; i++) {
    pool[seatShops[i][k]]--;
  }
}

const seatShopsWithNames = [];
for (let i = 0; i < 8; i++) {
  const names = [];
  for (const id of seatShops[i]) {
    names.push(CARDS[id]);
  }
  seatShopsWithNames.push(names);
}

console.log(seatSeeds);
console.log(pool);
console.log(seatShops);
console.log(seatShopsWithNames);

This implements the simultaneous method of dealing cards to multiple players described in the 2026-05-29 addendum to Beyond Duplicate Bridge, which I don't actually recommend; for that purpose, use randomNeedleFromHaystacks instead, as described in the 2026-06-01 addendum.

API

createHaystack(seed) → Haystack

  • seed must be an integer or bigint in [0, 2 ** 64), or a string representation thereof, such as '42' instead of 42. For testing, any number is fine. For production, use random numbers.
  • Haystack is a state object that cleanly serializes and deserializes with JSON.stringify and JSON.parse.

randomNeedle(haystack, weights) → needleId | null

  • weights is a JS object that maps needleId to probability weight. Weight values need only be in proportion to each other, and need not sum to 1.
  • needleIds must be positive integers other than 1. But they're mostly used as object keys, so in most places, the string representation is used, including in randomNeedle's return value.
  • Pathological combinations of needleIds are possible, but you're not gonna run into one unless it's deliberate. It's fine to start at 2 and count up, as seen in the demo code.
  • This mutates the haystack.
  • This returns null if and only if no weight is positive.

randomNeedles(haystack, weights, n, withReplacement = true) → needleId[]

  • Same as calling randomNeedle n times, with similar performance. Use whichever is convenient.
  • If withReplacement is false, then after each needle is chosen, its weight will be decremented by 1, before choosing the next needle. In this case, the weights object will be mutated! If all weights end up non-positive, the array returned may be shorter than n.

randomNeedleFromHaystacks(Record<string, { haystack: Haystack; weights: WeightMap }>) → [haystackId, needleId]

  • The string key is the haystackId. See test code for usage example.
  • Explanation in the 2026-06-01 addendum to Beyond Duplicate Bridge. (In the main use case, you want the same total weight in each haystack.)

createRarityGenerator(seed) → PCGState

randomRarity(pcgState, probabilities) → [number, PCGState]

  • Not part of the haystack implementation, but may be useful for autochess-like games.
  • shuffle-duplication depends on pcg, which exports PCGState. For convenience, shuffle-duplication re-exports PCGState, so you can import it from either.
  • PCGState is never mutated. Like pcg's randomInt, randomRarity returns a two-element array of the random outcome and the new state. Remember to store the new state over the old state!
  • probabilities is an array of the probabilities of each rarity. Unlike the weights, these probabilities must sum to 1. (Except that probabilities[0] is ignored, since it's enough to read all of the other probabilities.) The number returned by randomRarity is the randomly-chosen index in probabilities.
  • needleIds shouldn't be 1 because it's reserved for internal use by these functions. As long as you abide by that restriction, you can use the same set of seeds for createHaystack and createRarityGenerator. If you don't use these functions, you can use 1 as a needleId.
  • 2026-05-29 Addendum: I'd only recommend using these functions if it's almost always advantageous to roll a higher rarity. In general, for most games, including, hypothetically, TFT, I'd recommend using the haystack instead. The same haystack can be used as long as each rarity uses a needleId that isn't used elsewhere. Think of needleId as outcomeId.