vendredi 26 juillet 2019

What are pythonic methods for implementing random roll tables?

A random roll table is a table used for random generation in tabletop games and the like. While they can be very simple they can also trend towards the complex.

An unusual example of a random roll table would be Wheel of Fortune-style games where certain categories can have a higher chance of occurring.

I'm building a random generator for D&D 5e magic items (see here: https://www.reddit.com/r/dndnext/comments/bg0h46/5e_magic_item_generator/) but I've decided I want to weight the tables to prefer certain results certain times.

A very simple table may look like this:

(1d6)
1: First result
2: Second result
3: Third result
4: Fourth result
5: Fifth Result
6: Sixth result

This can be resolved pretty easily by using a list and randomising between 1 and 6.

A slightly more complex (but still 1d6-based) table may look like this:

(1d6)
1-3: First result
4-5: Second result
6: Third result

My tables are more likely to look like:

(1d20)
1-5: First result
6-15: Second result
16-17: Third result
18: Fourth result
19: Fifth result
20: Sixth result

These tables have higher "weighting" towards certain categories to allow for those categories to be selected more often. In my magic item example, swords should have 6-15 as a "metal" blade whereas 1-5 would be a "bone" blade and 20 would be something very unusual like a "darkness" blade or a "light" blade.

One possible option is to add a "weight" to each category and perform a calculation on each roll to see what weight it lands on by adding together the previous weights but this feels horribly clunky.

I tried to implement this item generator previously in PHP and I used a switch case with comparison operators on the cases to solve this but obviously this does not work in Python.

What would be the recommend Python design for this weighted table implementation or does anyone have any ideas for implementation whatsoever? I'm willing to rewrite my data structure completely to take advantage of any ideas.




Aucun commentaire:

Enregistrer un commentaire