osdir.com

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]# Python Probability

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Hello, I am new to Python and I?have an exercise which I struggle with. The question is: 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 experiment 0.35222 0.44444 0.20334 1000000 experiment 0.33988 0.43011 0.23001 Probability 0.34000 0.43000 0.23000 The aim of this task is to get acquainted with the classical probability field, the relative frequency and the relation of it to the probability.Programming goal: recalling the basic elements of Python programming and generating random numbers.Help: by loading the random package (import random) and calling?random.random()?one may get a random number between 0 and 1 (by uniform distribution). The code?int(random.random()*6)+1?gives back an integer number between 1 and 6. So I did like: import random def dice(): ? ? attacker_dice=[random.randint(1,6) for _ in range(3)] ? ? defender_dice=[random.randint(1,6) for _ in range(2)] ? ? a=max(attacker_dice) ? ? b=max(defender_dice) ? ? for i in range(1000): ? ? ? ? F=0 ? ? ? ? S=0 ? ? ? ? T=0 ? ? ? ? if a>b: ? ? ? ? ? ? F+=1 ? ? ? ? if a==b: ? ? ? ? ? ? S+=1 ? ? ? ? else: ? ? ? ? ? ? T+=1 and I wanted to divide the F,T,S with 6^5 and make a table. But its giving me zeros or 333. And to get 5digits after the zero i wanted to use?"%.5f " %First.? Could you help me to finish this, and tell me what am I doing wrong? Thank you

- Prev by Date:
**logging module - how to include method's class name when using %(funcName)** - Next by Date:
**Pretty printing dicts with compact=True** - Previous by thread:
**numpy use cases for where and out** - Next by thread:
**Python Probability** - Index(es):