View difference between Paste ID: LryLDniy and csh35UEX
SHOW: | | - or go back to the newest paste.
1
def simulateBattle(troop1, troop2, inc=0, inc2=0, nsim=1000): 
2
    # simulate nsim battles
3
    res = range(nsim)
4
    for i in range(nsim): 
5
        p1 = troop1; p2 = troop2; t = 1
6
        while p1 < p2 and p1 > 0: 
7
            if t > 1:
8
                p1 = p1 + inc
9
                p2 = p2 + inc2 
10
            else:
11
                t += 1    
12
            tp1 = p1             
13
            p1 = p1 - sum([random.random() < 0.6 for _ in range(p2)])
14
            p2 = p2 - sum([random.random() < 0.7 for _ in range(tp1)])
15
        # if troop count of attacker is equal to defender --> failed attack
16
        # if troop count of defender is lower than 0      --> succesful attack  
17
        res[i] = p1 <= 0 and p2 > 0
18
    # return proportion of succesfull breaks
19
    return sum(res)/(nsim*1.0)