Roll.js, an Exact Dice-Simulation Library

Are you the sort of person who likes to play around with RPG or boardgame mechanics? Have you ever looked at some dice-based mechanic and wondered just what the outcome chances really were, but instead just gave up, rolled a couple of times, and relied…


This content originally appeared on Tab Completion and was authored by Tab Atkins Jr.

Are you the sort of person who likes to play around with RPG or boardgame mechanics? Have you ever looked at some dice-based mechanic and wondered just what the outcome chances really were, but instead just gave up, rolled a couple of times, and relied on vibes? Have you tried using AnyDice.com but gave up when you saw you'd have to learn a funky DSL to do anything non-trivial? Do you know JavaScript?

If you answered yes to some of those questions, I've got a new library for you! Roll.js is an exact-results dice-simulation library - it doesn't do simulations of the "repeat 10k times and report the average", it tracks every outcome with its precise chance of occurring (up to floating-point accuracy, at least).

The README explains the library itself, and there's a playground tool that'll let you write code against it immediately and even share the results with others! There are several bits of example code in the README, and several more in the playground (click the ? in the upper-right).

For example, a simple "d20 with advantage" roll is:

Roll.d20.advantage()

(playground link)

Want to know average damage of a greatsword after Great Weapon Master is applied (re-roll 1s and 2s, a single time)?

Roll.nd(2, 6).replace(x=> x <= 2, Roll.d6).sum();

(playground link)

Wanna build a d5 out of a d6 by rerolling 6s until they stop coming up, as long as it takes?

// "reroll()" calls map on its returned Roll 
// results again, until things stabilize.
Roll.d6.reroll({
  map: x=> (x == 6) ? Roll.d6 : x
});

(playground link)

Wanna do something complicated, like figure out what the chances are of dying/stabilizing/reviving from a D&D death save?

Roll.d20.reroll({
	summarize(roll, oldSummary={}) {
		return {
			successes:(roll>=10?1:0) + (oldSummary.successes || 0),
			failures:(roll<10?1:0) + (roll==1?1:0) + (oldSummary.failures || 0),
			nat20: (roll==20),
			toString() { return `${this.successes}/${this.failures}/${this.nat20}`; },
		}
	},
	map(summary) {
		if(summary.nat20) return "revive";
		if(summary.successes >= 3) return "stabilize";
		if(summary.failures >= 3) return "die";
		return Roll.d20;
	}
})

(playground link)


Point is, this library can do a lot of stuff, pretty easily, and you can use the JS you already know to do arbitrarily complicated stuff with it. I originally wrote it because I was fed up rewriting simulation code every time my brother and I were thinking about D&D homebrew, and in particular when I wrote some code to test out an alternate death-save mechanic it was getting too complicated; I figured I could just do it right, once, and (after several days of swearing at infinite-looping reroll code) I was correct!

At the moment the convenience functions (like .advantage()) are pretty biased towards D&D 5e usage, but I'm happy to add more for other dice systems. If you have any you'd like to see, let me know in a comment!


This content originally appeared on Tab Completion and was authored by Tab Atkins Jr.


Print Share Comment Cite Upload Translate Updates
APA

Tab Atkins Jr. | Sciencx (2022-08-12T16:53:25+00:00) Roll.js, an Exact Dice-Simulation Library. Retrieved from https://www.scien.cx/2022/08/12/roll-js-an-exact-dice-simulation-library/

MLA
" » Roll.js, an Exact Dice-Simulation Library." Tab Atkins Jr. | Sciencx - Friday August 12, 2022, https://www.scien.cx/2022/08/12/roll-js-an-exact-dice-simulation-library/
HARVARD
Tab Atkins Jr. | Sciencx Friday August 12, 2022 » Roll.js, an Exact Dice-Simulation Library., viewed ,<https://www.scien.cx/2022/08/12/roll-js-an-exact-dice-simulation-library/>
VANCOUVER
Tab Atkins Jr. | Sciencx - » Roll.js, an Exact Dice-Simulation Library. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/08/12/roll-js-an-exact-dice-simulation-library/
CHICAGO
" » Roll.js, an Exact Dice-Simulation Library." Tab Atkins Jr. | Sciencx - Accessed . https://www.scien.cx/2022/08/12/roll-js-an-exact-dice-simulation-library/
IEEE
" » Roll.js, an Exact Dice-Simulation Library." Tab Atkins Jr. | Sciencx [Online]. Available: https://www.scien.cx/2022/08/12/roll-js-an-exact-dice-simulation-library/. [Accessed: ]
rf:citation
» Roll.js, an Exact Dice-Simulation Library | Tab Atkins Jr. | Sciencx | https://www.scien.cx/2022/08/12/roll-js-an-exact-dice-simulation-library/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.