Package: icepool
IcepoolPython dice probability package.GitHub repository.PyPi page.Try coding in your browser usingIcecup, a simple frontend for scripting and graphing similar to AnyDice, SnakeEyes, and Troll. You can find a series of tutorialshere.FeaturesPure Python implementation using only the Standard Library. Run it almost anywhere Python runs: program locally, share Jupyter notebooks, or build your own client-side web apps using Pyodide.Dice support all standard operators (+, -, <, >, etc.) as well as an extensive library of functions (rerolling, exploding, etc.)Efficient dice pool algorithm can solve keep-highest, finding sets and/or straights,RISK-like mechanics, and more in milliseconds, even for large pools.Exact fractional probabilities using Pythonints.Some support for decks (aka sampling without replacement).Installingpip install icepoolThe source is pure Python, so including a direct copy in your project can work as well.ContactFeel free to open adiscussionorissueon GitHub. You can also find me onRedditorTwitter.API documentationpdoc on GitHub.JupyterLite notebooksSee thisJupyterLite distributionfor a collection of interactive, editable examples. These include mechanics from published games, StackExchange, Reddit, and academic papers.JupyterLite REPL.Tutorial notebooksIn particular, here is a series oftutorial notebooks.Web applicationsThese are all client-side, powered byPyodide. Perhaps you will find inspiration for your own application.Icecup, a simple frontend for scripting and graphing.Alex Recarey'sFace 2 Face CalculatorforInfinity the Game N4-- including the ability to be installed on Android and iOS and run without an internet connection.Ability score rolling method calculator.Cortex Prime calculator.Legends of the Wulincalculator.Year Zero Engine calculator.Paper on algorithmPresented atArtificial Intelligence and Interactive Digital Entertainment (AIIDE) 2022.In the official proceedings.Preprint in this repository.BibTeX:@inproceedings{liu2022icepool,title={Icepool: Efficient Computation of Dice Pool Probabilities},author={Albert Julius Liu},booktitle={Eighteenth AAAI Conference on Artificial Intelligence and Interactive Digital Entertainment},volume={18},number={1},pages={258-265},year={2022},month={Oct.},eventdate={2022-10-24/2022-10-28},venue={Pomona, California},url={https://ojs.aaai.org/index.php/AIIDE/article/view/21971},doi={10.1609/aiide.v18i1.21971}}VersioningFrankly, backwards compatability is not a high priority. If you need specific behavior, I recommend version pinning. I believe theDieinterface to be reasonably stable, but there's a good chance thatMultiset*will see more changes in the future. Typing is especially unstable.Inline examplesSumming ability scoresWhat's the chance that the sum of player A's six ability scores is greater than or equal to the sum of player B's six ability scores?
(Using 4d6 keep highest 3 for each ability score.)importicepoolsingle_ability=icepool.d6.highest(4,3)# The @ operator means: compute the left side, and then roll the right side that many times and sum.print(6@single_ability>=6@single_ability)Die with denominator 22452257707354557240087211123792674816OutcomeProbabilityFalse47.984490%True52.015510%All matching setsBlog post.Question on Reddit.Another question on Reddit.Question on StackExchange.Roll a bunch of dice, and findallmatching sets (pairs, triples, etc.)Wecouldmanually enumerate every case as per the blog post. However, this is prone to error.
Fortunately, Icepool can do this simply and reasonably efficiently with no explicit combinatorics on the user's part.importicepoolclassAllMatchingSets(icepool.MultisetEvaluator):defnext_state(self,state,outcome,count):"""next_state computes a "running total"given one outcome at a time and how many dice rolled that outcome."""ifstateisNone:state=()# If at least a pair, append the size of the matching set.ifcount>=2:state+=(count,)# Prioritize larger sets.returntuple(sorted(state,reverse=True))all_matching_sets=AllMatchingSets()# Evaluate on 10d10.print(all_matching_sets(icepool.d10.pool(10)))Die with denominator 10000000000OutcomeQuantityProbability()36288000.036288%(2,)1632960001.632960%(2, 2)114307200011.430720%(2, 2, 2)190512000019.051200%(2, 2, 2, 2)7144200007.144200%(2, 2, 2, 2, 2)285768000.285768%(3,)2177280002.177280%(3, 2)152409600015.240960%(3, 2, 2)190512000019.051200%(3, 2, 2, 2)3810240003.810240%(3, 3)3175200003.175200%(3, 3, 2)3810240003.810240%(3, 3, 2, 2)317520000.317520%(3, 3, 3)141120000.141120%(4,)1270080001.270080%(4, 2)4762800004.762800%(4, 2, 2)2857680002.857680%(4, 2, 2, 2)158760000.158760%(4, 3)1270080001.270080%(4, 3, 2)635040000.635040%(4, 3, 3)15120000.015120%(4, 4)79380000.079380%(4, 4, 2)11340000.011340%(5,)381024000.381024%(5, 2)762048000.762048%(5, 2, 2)190512000.190512%(5, 3)127008000.127008%(5, 3, 2)18144000.018144%(5, 4)9072000.009072%(5, 5)113400.000113%(6,)63504000.063504%(6, 2)63504000.063504%(6, 2, 2)4536000.004536%(6, 3)6048000.006048%(6, 4)189000.000189%(7,)6048000.006048%(7, 2)2592000.002592%(7, 3)108000.000108%(8,)324000.000324%(8, 2)40500.000041%(9,)9000.000009%(10,)100.000000%Similar projectsIn roughly chronological order:Troll by Torben Ægidius Mogensenhttp://hjemmesider.diku.dk/~torbenm/Troll/The oldest general-purpose dice probability calculator I know of. It has an accompanying peer-reviewed paper.AnyDice by Jasper Flickhttps://anydice.com/Probably the most popular dice probability calculator in existence, and with good reason---its accessibility and shareability remains unparalleled. I still use it often for prototyping and as a second opinion.SnakeEyes by Noé Falzonhttps://snake-eyes.io/SnakeEyes demonstrated the viability of browser-based, client-side dice calculation, as well as introducing me toChart.js.dice_roll.py by Ilmari Karonenhttps://gist.github.com/vyznev/8f5e62c91ce4d8ca7841974c87271e2fThis demonstrated the trick of iterating "vertically" over the outcomes of dice in a dice pool, rather than "horizontally" through the dice---one of the insights into creating a much faster dice pool algorithm.dyceby Matt Bogosianhttps://github.com/posita/dyceAnother Python dice probability package. I've benefited greatly from exchanging our experiences.