Return probability list of all possible sums when throwing dice. >>> random.seed(0) >>> throw_dice(10, 1) [10.0, 0.0, 30.0, 50.0, 10.0, 0.0] >>> throw_dice(100, 1) [19.0, 17.0, 17.0, 11.0, 23.0, 13.0] >>> throw_dice(1000, 1) [18.8, 15.5, 16.3, 17.6, 14.2, 17.6]
(num_throws: int, num_dice: int = 2)
| 15 | |
| 16 | |
| 17 | def throw_dice(num_throws: int, num_dice: int = 2) -> list[float]: |
| 18 | """ |
| 19 | Return probability list of all possible sums when throwing dice. |
| 20 | |
| 21 | >>> random.seed(0) |
| 22 | >>> throw_dice(10, 1) |
| 23 | [10.0, 0.0, 30.0, 50.0, 10.0, 0.0] |
| 24 | >>> throw_dice(100, 1) |
| 25 | [19.0, 17.0, 17.0, 11.0, 23.0, 13.0] |
| 26 | >>> throw_dice(1000, 1) |
| 27 | [18.8, 15.5, 16.3, 17.6, 14.2, 17.6] |
| 28 | >>> throw_dice(10000, 1) |
| 29 | [16.35, 16.89, 16.93, 16.6, 16.52, 16.71] |
| 30 | >>> throw_dice(10000, 2) |
| 31 | [2.74, 5.6, 7.99, 11.26, 13.92, 16.7, 14.44, 10.63, 8.05, 5.92, 2.75] |
| 32 | """ |
| 33 | dices = [Dice() for i in range(num_dice)] |
| 34 | count_of_sum = [0] * (len(dices) * Dice.NUM_SIDES + 1) |
| 35 | for _ in range(num_throws): |
| 36 | count_of_sum[sum(dice.roll() for dice in dices)] += 1 |
| 37 | probability = [round((count * 100) / num_throws, 2) for count in count_of_sum] |
| 38 | return probability[num_dice:] # remove probability of sums that never appear |
| 39 | |
| 40 | |
| 41 | if __name__ == "__main__": |