Skip to content

🎲 Dice that protects you from time travelers!

Notifications You must be signed in to change notification settings

phil-r/unfair-dice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

(Un)fair dice

Dice that protects you from time travelers!

Description

Imagine a simple coin. Is it possible to always get heads? Yes! Well.. if you can time travel of course, just travel back and re-roll until you get heads(in computer gaming it's called save scumming), continue ad infinitum.

To solve this problem (un)fair dice was invented! It keeps track of every roll and adjusts outcomes based on previous rolls. Time traveller will die of old age trying to scam this!

Usage

const { createCoin } = require('unfair-dice');
const coin = createCoin(); // or createDice(2)
console.log(coin.flip()); // 0 or 1

or using array:

const { createDice } = require('unfair-dice');
const coin = createDice(['heads', 'tails']);
console.log(coin.flip()); // heads or tails

or create a d6:

const { createDice } = require('unfair-dice');
const d6 = createDice(6);
console.log(d6.roll()); // 0, 1, 2, 3, 4 or 5

State

const { createDice, createFromState } = require('unfair-dice');
const d6 = createDice(6);
d6.roll();
// ...
const saveState = d6.getState(); // save it somewhere as json, etc..
const d6Clone = createFromState(saveState); // recover it from state