Advertisement
Guest User

Untitled

a guest
Sep 5th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. #!/usr/bin/python3 -OO
  2.  
  3. import random
  4. import math
  5.  
  6. def sample(ex,hp):
  7.     greens, reds = 15, 15 # start w/ 15 tanks each
  8.     rHP = [hp] * reds
  9.     gHP = [hp] * greens
  10.     while greens > 0 and reds > 0:
  11.         green_power, red_power = greens**ex, reds**ex # Lanchester’s Square Law
  12.         prob_red_gets_hit = green_power / (green_power + red_power)
  13.         rand = random.random()
  14.         if rand < prob_red_gets_hit:
  15.             hit_tank = math.floor(random.random() * reds)
  16.             rHP[hit_tank] -= 1
  17.             if rHP[hit_tank] == 0:
  18.                 del rHP[hit_tank]
  19.                 reds -= 1
  20.         else:
  21.             hit_tank = math.floor(random.random() * greens)
  22.             gHP[hit_tank] -= 1
  23.             if gHP[hit_tank] == 0:
  24.                 del gHP[hit_tank]
  25.                 greens -= 1
  26.     return reds - greens
  27.  
  28. for e in range (0,13):
  29.     for hp in range (1,6):
  30.         ex = e * 0.25
  31.         histogram = [0] * 16
  32.         for sample_count in range(0,1000000):
  33.             victory_margin = abs(sample(ex,hp))
  34.             histogram[victory_margin] += 1
  35.  
  36.         print(ex, ",", hp, ",", histogram[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement