git.net

[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]

On 09/11/2018 11:54 AM, endzis at freemail.hu wrote: > 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 Every time through this loop, you set F, S and T to zero.? If you want those variables to accumulate values, move the three initialization lines to before the loop: F = 0 S = 0 T = 0 for ... ?? if a>b: ???? F += 1 ?? ... and so on ... But you have another problem.? You simulate rolling the dice only once.? For your 1000 trials, you need to roll the dice 1000 times. The first few lines that simulate the dice roll must be inside the loop so that each pass through the loop rolls the dice. > 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 -- Dr. Gary Herron Professor of Computer Science DigiPen Institute of Technology (425) 895-4418

- Prev by Date:
**Python Probability** - Next by Date:
**"glob.glob('weirdness')" Any thoughts?** - Previous by thread:
**Python Probability** - Index(es):