It is necessary to display the top 3 results of rolling 2 dice, which have a variable number of sides

The following code calculates the probability distribution of the results of rolling two dice with a variable number of equal sides:

def compute_probability_distribution(sides):
  dist = {x+y: 0 for x in range(1, sides+1) for y in range(1, sides+1)}
  for die_1 in range(1, sides+1):
    for die_2 in range(1, sides+1):
      dist[die_1+die_2] = dist[die_1+die_2] + 1
  probs = dist.items()
  print "Prob dist: ", probs

      

For example, for regular 6-sided dice, the trial distance is [(2.6), (3.2), (4.3), (5.4), (6.5), (7.6), (8 , 5),) (9,4), (10,3), (11,2), (12,1)], where the first element of each set is the sum of two cubes and the second element is the number of ways that can happen in one roll. Can anyone tell me how to sort the above poll list by the second element of each tuple so that I can output the top (1 or 3) most likely cases? I mean using the built in sort list with some sort of comparison function.

+3


source to share


3 answers


probs = [(2,6),(3,2),(4,3),(5,4),(6,5),(7,6),(8,5),(9,4),(10,3),(11,2),(12,1)]

>>> sorted(probs, key=lambda x: x[1])  # x[1] is second element of tuple pair.
[(12, 1),
 (3, 2),
 (11, 2),
 (4, 3),
 (10, 3),
 (5, 4),
 (9, 4),
 (6, 5),
 (8, 5),
 (2, 6),
 (7, 6)]

      



+2


source


You can do this with nested concepts, but you can also calculate the most common values โ€‹โ€‹manually if you know the number of sides.

In ascending order of probability:

  • 2

    and sides+sides

    that only have one chance.
  • Then 3

    and sides+nsides-1

    that have 2.
  • 4

    and sides+nsides-2

    have 3
  • ...
  • Finally sides+1

    has the highest probability, which is sides

    .

If you don't believe me, look at the probability distribution for different numbers of sides.



So, to get the 3 most common values, you can simply calculate them based on the number of sides:

def compute_probability_distribution(sides):
    print([(sides+1, sides), (sides, sides-1), (sides+2, sides-1)])

      

However, this only works for cubes with at least 2 sides. For one side bone, the result will be strange with this function.

+2


source


I would just use the data structure designed for this: a Counter

:

from collections import Counter

def compute_probability_distribution(sides):
  dist = Counter(die_1 + die_2 for die_1 in range(1, sides+1)
                               for die_2 in range(1, sides+1))
  probs = dist.most_common(3)
  print "Prob dist: ", probs
      

For two 6-dice, this will produce:

>>> compute_probability_distribution(6)
Prob dist:  [(7, 6), (6, 5), (8, 5)]

      

So we got six times the sum of seven; five times six; and five times eight.

If you want to make the number of dice arbitrary, you can use:

from collections import Counter
from itertools import product

def compute_probability_distribution(sides,ndices=2,common=3):
  dist = Counter(sum(d) for d in product(range(1,sides+1),repeat=ndices))
  probs = dist.most_common(common)
  print "Prob dist: ", probs
      

So now we can calculate the 10 most common sums when we roll three 5-dice:

>>> compute_probability_distribution(5,3,10)
Prob dist:  [(9, 19), (8, 18), (10, 18), (7, 15), (11, 15), (6, 10), (12, 10), (5, 6), (13, 6), (4, 3)]

      

+1


source







All Articles