de Méré's paradox

https://carnotcycle.files.wordpress.com/2017/11/mere01.jpg?w=614&h=458

Chevalier de Méré (Antoine Gombaud 1607-1684) was a big spender in gambling circles. He found that it is worth to bet on the event of getting at least one 6 in four rolls of a die. He thought (misleaded by wrong calculation) that rolling two dice 24 times with a bet on having at least one double 6 has the same winning chance. But he found by experimenting that it is not true.

He wrote in 1654 to Blaise Pascal (1623-1662), who in turn shared the news of “De Méré’s paradox” with Pierre Fermat (1607-1665). They solved the “paradox” and with this the theory of probability was born.

Simulation with 4 dice (relative frequency)

Calculate the relative frequency of at least one 6 in four rolls of a die!

Run the next code several times! Does it seem true that the relative frequency is more times greater than 0.5 than less, i.e. that it is worth betting that there will be at least one 6 among the 4 dice rolls?

Probability with 4 dice

Calculate the exact probability of at least one 6 in four rolls of a die!

The next is almost the same code but there is a big difference in running. Instead of list comprehension use generator expression, as it does not construct a big list. It generates the elements when needed, that is in the for loop.

Compair lst and gen:

Example: Write a complete program putting the codes above into functions (simulation and calculating probability) and call them in the main funcion! Save this code into a file and run it from terminal!

Using a little mathematics, we could calculate the probability without listing all possibilities:

A programming tip: listing the results of several embedded loops we may use the product function of itertools module.

Simulation and probability with 24 times 2 dice

Checking all possibilities and counting the number of double 6 is impossible, so we need to use math!

Exercise: For practice, write a complete program (similar to the one above) for the case of two dice rolled 24 times!

Fill in and rewrite the next code:

Risk (deadline 2021-09-18 20:00)

In the strategic board game called Risk, one player can attack up to three soldiers simultaneously, while the defending player can defend up to two. In the case of exactly three attackers and two defenders, the collision is as follows. An attacking player rolls three red dice while the defending player rolls two blue dice. Then they compare the bigest throws of the attacker and the defender. The lesser value loses a soldier, in the case of equal values the attacker loses one soldier. Then the second largest numbers are also compared in the same way. Thus, the battle has three outcomes: the attacker loses two soldiers, each side loses 1-1 soldiers, the defender loses two soldiers. Simulate 1000 times the experiment and determine the relative frequency of the three events. Simulate 1000000 times the experiment and determine the relative frequency of the three events. Calculate the exact probability of the three outcomes by examining all possible cases. The probability is the ratio of the favorable cases and the total number of cases. Write these results with 5 decimal places leaving 3 spaces between them! The output of the program looks like this (of course with other numbers)

                     Attacker  Draw      Defender
1000 experiments     0.35200   0.44400   0.20400 
1000000 experiments  0.33988   0.43011   0.23001  
Probability          0.34000   0.43000   0.23000

Upload your solution in teams, the file must be a text file with the extension .py. The first lines of the code should be:

"""
I encoded this program myself, did not copy or rewrite the code of others,
and did not give or send it to anyone else.  
Your Full Name
"""