Dice Probability Calculator puzzle discussion


#1

Hi

I got the notification about this community puzzle and got interested. It's a nice idea, and the "puzzle" part is mainly the solution implementation strategy (some interesting options exist).

However, I got stuck on validation case 5.
The probability of several of the results is 3.125% (1 out of 32 cases), and the requirement call for rounding the result to 2 places after the decimal point. So my program output was 3.13, which is indeed the correct rounding.

But the validator expects 3.12 ?????

I hardcoded this case into my code, just to view other solutions, and it seems to me that other solutions also output 3.13...

Would appreciate any insight into this :slight_smile:


Dice-probability-calculator
#2

It's one correct rounding, but unfortunately multiple could make sense and the puzzle doesn't specify. Out of curiosity, what language and rounding method are you using?

About validation case 5 "Subtraction of dice", it doesn't have an odd multiple of 0.005 as an unrounded solution, so you shouldn't have that problem.

I'll see if I can eliminate the others I can find. Done. It was only test 5. No validator was impacted.


#3

Thanks for the quick reply.
I'm using Javascript, and tried several combination of Math.round() and toFixed(). All output the (mathematically) correct answer of 3.13

In test case 5, there are four distinct values that result in probability of 3.125: -7, -6, 5, 6. Each appears exactly once out of the 32 possible dice combinations (1/32 = 3.125%)

EDIT - regarding the rounding:
The rules specify "Floats are formatted with 2 decimal figures, rounded"
The mathematical rule for rounding is
* If the number you are rounding is followed by 5, 6, 7, 8, or 9, round the number up
* If the number you are rounding is followed by 0, 1, 2, 3, or 4, round the number down

As far as I know, this is a universal definition, not a language-specific implementation.

EDIT 2 - rounding can indeed be a rabbit hole
https://www.mathsisfun.com/numbers/rounding-methods.html


#4

I just saw that test case 5 was changed to avoid this problem. Thanks !

(original expression was "d8-2*d4", it is now "d9-2*d4")


#5

LOL. Good for you, you edited before I got back to reading.

Yup, I did that. I notified in the problem's backroom comment box, but forgot to come back and announce it here.

I'm surprised this one didn't work, but indeed the author (and most of us AFAIK) used printf formatting, which rounds without specifying too much either; and the MDN docs do mention JS Math.round differs from common languages.

In any case, this wasn't a problem about rounding, so good riddance.


#6

Hello,

I an coding in JAVA.

I can’t get “FIX IT” test, I know that my code is not optimized but I don’t know what. Note that sometime the test is OK but when I submit, it failed at “FIX IT”.

I am using a recursive function to interprete d’n’ (with Pattern/Matcher) in order to extract all possibles outputs (note that I also compute propability simply by dividing by the number n. (Everything work perfectly).

I am using TreeMap in order to automatic sort of output values.

What have i missed? Any hint in order to optimize my code?

Thanks,