python-dice icon indicating copy to clipboard operation
python-dice copied to clipboard

Support for floating point output

Open saranvdev opened this issue 6 years ago • 7 comments

What do you think? things like roll(1d2/3) would return 0.3(3) or 0.6(6)

saranvdev avatar Jun 28 '18 13:06 saranvdev

I don't think I want to support this, since dice and decimal numbers don't mix in my experience. @borntyping thoughts?

Floating point numbers can also be hairy to work with. Can you point to any systems or games that could make use of this feature?

calebj avatar Jun 30 '18 00:06 calebj

I'm not sure what you'd be able to use it for. If we did add it, maybe we could do it as a Python-2 style "true division" operator using // as the symbol. It might break other things though, as IIRC a lot of the other operators expect an integer.

borntyping avatar Jul 01 '18 11:07 borntyping

I wanted it for things like calculating damage done, including fire resistance for example. 75dmg/0.10% = 7.5 blocked damage. I need to cover all possible cases of rolls, because I don't know what players will use.

It's just an idea, though

saranvdev avatar Jul 01 '18 11:07 saranvdev

If we did add it, maybe we could do it as a Python-2 style "true division" operator using // as the symbol.

Or what about optional argument? roll("1d10/20", float=True), something like this, I don't know how many changes are needed to support such feature.

saranvdev avatar Jul 04 '18 22:07 saranvdev

dyce will do this. It's pretty flexible on what it accommodates as outcome (face) types:

>>> from dyce import H
>>> 1@H(2) / 3
H({0.3333333333333333: 1, 0.6666666666666666: 1})
>>> _.roll()
0.3333333333333333

>>> from fractions import Fraction
>>> 1@H(2) / Fraction(3)
H({Fraction(1, 3): 1, Fraction(2, 3): 1})
>>> _.roll()
Fraction(2, 3)

You'd have to settle on a grammar, but you might be able to lean on it under the covers if you can identify something you like.

I'm happy to consult on this, if there's interest.

posita avatar Aug 17 '21 11:08 posita

dyce looks really neat. If I ever rewrite this library (I've not really touched it in a long time) it'd make doing a lot of the analysis I originally wanted to be able to do with it possible. Has anyone else done built a dice parser/grammer on top of dyce yet?

(side note: you've cited me as Clemens et al. in the docs - that should be Clements et al. if that's something easy to fix 😅)

borntyping avatar Aug 17 '21 12:08 borntyping

Oh dear! 🤦 How embarrassing…. ~I'll have a fix for that soon.~ Should be fixed. Thanks for bringing this to my attention! 😊

To get to the meat of your question: The only person who has tried so far is me, and so far in private. That has been helpful, but eating one's own dog food only takes one so far. The real test is whether it's helpful to others.

My original goal was to support finite discrete distributions to assist with dice mechanic design (much like what AnyDice does). But then I realized that there were some limitations that don't suit all cases. For example, consider when someone wants to roll sixteen d20s and take the best five. If the approach is to enumerate all outcome probabilities and then pick from those, it probably isn't getting done anytime soon. (Computationally, it's just too expensive.) That got me thinking about cases where a particular roll was important, not the entire distribution. That meant deferring the math until roll time. That, and I noticed that avrae/d20 has a capability not only to generate rolls, but dissect their constructions, which is also neat.

So (at least as of this writing) dyce now has three goals:

  1. Assist with mechanics design through distribution enumeration
  2. Generate weighted random rolls that aren't bound by distribution computation for use at game time
  3. Support transparency of computation for roll generation (for auditing, education, etc.)

I think the first is pretty well baked. (Time will tell.) The second two that are pretty new efforts. The hope is that one could build specialized grammars that leveraged dyce in any of those contexts, depending on what one was trying to accomplish.

posita avatar Aug 17 '21 14:08 posita