Simulações de Monte Carlo em Python
Izzy Weber
Curriculum Manager, DataCamp
Simulações de Monte Carlo são usadas em áreas como...





Role dois dados separados de duas bolsas, cada uma com três dados viciados:
bag1 = [[1, 2, 3, 6, 6, 6], [1, 2, 3, 4, 4, 6], [1, 2, 3, 3, 3, 5]]
bag2 = [[2, 2, 3, 4, 5, 6], [3, 3, 3, 4, 4, 5], [1, 1, 2, 4, 5, 5]]
Simulação:
def roll_biased_dice(n): results = {}for i in range(n): bag_index1 = random.randint(0, 2) die_index1 = random.randint(0, 5) bag_index2 = random.randint(0, 2) die_index2 = random.randint(0, 5)point1 = bag1[bag_index1][die_index1] point2 = bag2[bag_index2][die_index2]key = "%s_%s" % (point1, point2)if point1 + point2 == 8: if key not in results: results[key] = 1 else: results[key] += 1
| dice1_dice2 | probability_of_success |
|---|---|
| 6_2 | 5.54 |
| 3_5 | 2.67 |
| 2_6 | 1.45 |
| 4_4 | 4 |
bag1 = [[1, 2, 3, 6, 6, 6], [1, 2, 3, 4, 4, 6], [1, 2, 3, 3, 3, 5]]
bag2 = [[2, 2, 3, 4, 5, 6], [3, 3, 3, 4, 4, 5], [1, 1, 2, 4, 5, 5]]
Resultados da simulação com 10.000 execuções:

bag1 = [[2, 2, 3, 4, 6, 6], [1, 2, 2, 4, 6, 6], [1, 2, 3, 3, 3, 3]]
bag2 = [[1, 2, 3, 4, 5, 6], [1, 3, 3, 4, 4, 6], [2, 2, 2, 3, 5, 5]]
Resultados da simulação com 10.000 execuções:

Simulações de Monte Carlo em Python