https://carnotcycle.files.wordpress.com/2017/11/mere01.jpg?w=614&h=458
Chevalier de Méré (Antoine Gombaud 1607-1684) nagy szerencsejátékos volt. Azt találta, hogy érdemes arra fogani, hogy 4 kockadobásból legalább az egyik 6-os lesz. Helytelenül, arányokra hivatkozva azt gondolta, hogy érdemes arra fogadni, hogy 24-szer feldobva 2 kockát lesz egy dupla 6-os. A tapasztalatai azonban ezt nem igazolták!
1654-ben levelet írt Blaise Pascalnak (1623-1662), aki megosztotta e paradoxonnak látszó kérdést Pierre Fermat-val (1607-1665). Megoldották e problémát, és ezt a pillanatot tekintik sokan a valószínűségszámítás kezdetének.
from random import randint
seq = [randint(1,6) for i in range(4)]
seq
[1, 2, 3, 2]
Futassuk le a következő kódot többször is! Igaznak tűnik, hogy a relatív gyakoriság többször nagyobb 0.5-nél mint ahányszor kisebb, vagyis hogy érdemes arra fogadni, hogy lesz a 4 dobás közt legalább egy 6-os?
n = 100
count = 0
for _ in range(n):
seq = [randint(1,6) for i in range(4)]
if 6 in seq:
count += 1
print(count/n)
0.57
Számítsuk ki az összes eset felsorolásával és a kedvező esetek megszámolásával annak valószínűségét, hogy 4 kockadobás közt lesz legalább egy 6-os.
lst = [[i1, i2, i3, i4] for i1 in range(1,7) for i2 in range(1,7) \
for i3 in range(1,7) for i4 in range(1,7)]
count = 0
for i in lst:
if 6 in i:
count += 1
print(count, 6**4, count/6**4)
671 1296 0.5177469135802469
A következő kód majdnem azonos az előzővel, a különbség futás közben mégis nagy. Listaértelmezés esetén legenerálódik az összes lehetséges eredmény egy listába, míg a következő generátorkifejezés használatakor nem keletkezik lista, minden számnégyest csak akkor számít ki, amikor a for
ciklusban szükség van rá:
gen = ([i1, i2, i3, i4] for i1 in range(1,7) for i2 in range(1,7) \
for i3 in range(1,7) for i4 in range(1,7))
count = 0
for i in gen:
if 6 in i:
count += 1
print(count, 6**4, count/6**4)
671 1296 0.5177469135802469
Hasonlítsuk össze az lst
és a gen
változók tartalmát:
lst
[[1, 1, 1, 1], [1, 1, 1, 2], [1, 1, 1, 3], [1, 1, 1, 4], [1, 1, 1, 5], [1, 1, 1, 6], [1, 1, 2, 1], [1, 1, 2, 2], [1, 1, 2, 3], [1, 1, 2, 4], [1, 1, 2, 5], [1, 1, 2, 6], [1, 1, 3, 1], [1, 1, 3, 2], [1, 1, 3, 3], [1, 1, 3, 4], [1, 1, 3, 5], [1, 1, 3, 6], [1, 1, 4, 1], [1, 1, 4, 2], [1, 1, 4, 3], [1, 1, 4, 4], [1, 1, 4, 5], [1, 1, 4, 6], [1, 1, 5, 1], [1, 1, 5, 2], [1, 1, 5, 3], [1, 1, 5, 4], [1, 1, 5, 5], [1, 1, 5, 6], [1, 1, 6, 1], [1, 1, 6, 2], [1, 1, 6, 3], [1, 1, 6, 4], [1, 1, 6, 5], [1, 1, 6, 6], [1, 2, 1, 1], [1, 2, 1, 2], [1, 2, 1, 3], [1, 2, 1, 4], [1, 2, 1, 5], [1, 2, 1, 6], [1, 2, 2, 1], [1, 2, 2, 2], [1, 2, 2, 3], [1, 2, 2, 4], [1, 2, 2, 5], [1, 2, 2, 6], [1, 2, 3, 1], [1, 2, 3, 2], [1, 2, 3, 3], [1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 3, 6], [1, 2, 4, 1], [1, 2, 4, 2], [1, 2, 4, 3], [1, 2, 4, 4], [1, 2, 4, 5], [1, 2, 4, 6], [1, 2, 5, 1], [1, 2, 5, 2], [1, 2, 5, 3], [1, 2, 5, 4], [1, 2, 5, 5], [1, 2, 5, 6], [1, 2, 6, 1], [1, 2, 6, 2], [1, 2, 6, 3], [1, 2, 6, 4], [1, 2, 6, 5], [1, 2, 6, 6], [1, 3, 1, 1], [1, 3, 1, 2], [1, 3, 1, 3], [1, 3, 1, 4], [1, 3, 1, 5], [1, 3, 1, 6], [1, 3, 2, 1], [1, 3, 2, 2], [1, 3, 2, 3], [1, 3, 2, 4], [1, 3, 2, 5], [1, 3, 2, 6], [1, 3, 3, 1], [1, 3, 3, 2], [1, 3, 3, 3], [1, 3, 3, 4], [1, 3, 3, 5], [1, 3, 3, 6], [1, 3, 4, 1], [1, 3, 4, 2], [1, 3, 4, 3], [1, 3, 4, 4], [1, 3, 4, 5], [1, 3, 4, 6], [1, 3, 5, 1], [1, 3, 5, 2], [1, 3, 5, 3], [1, 3, 5, 4], [1, 3, 5, 5], [1, 3, 5, 6], [1, 3, 6, 1], [1, 3, 6, 2], [1, 3, 6, 3], [1, 3, 6, 4], [1, 3, 6, 5], [1, 3, 6, 6], [1, 4, 1, 1], [1, 4, 1, 2], [1, 4, 1, 3], [1, 4, 1, 4], [1, 4, 1, 5], [1, 4, 1, 6], [1, 4, 2, 1], [1, 4, 2, 2], [1, 4, 2, 3], [1, 4, 2, 4], [1, 4, 2, 5], [1, 4, 2, 6], [1, 4, 3, 1], [1, 4, 3, 2], [1, 4, 3, 3], [1, 4, 3, 4], [1, 4, 3, 5], [1, 4, 3, 6], [1, 4, 4, 1], [1, 4, 4, 2], [1, 4, 4, 3], [1, 4, 4, 4], [1, 4, 4, 5], [1, 4, 4, 6], [1, 4, 5, 1], [1, 4, 5, 2], [1, 4, 5, 3], [1, 4, 5, 4], [1, 4, 5, 5], [1, 4, 5, 6], [1, 4, 6, 1], [1, 4, 6, 2], [1, 4, 6, 3], [1, 4, 6, 4], [1, 4, 6, 5], [1, 4, 6, 6], [1, 5, 1, 1], [1, 5, 1, 2], [1, 5, 1, 3], [1, 5, 1, 4], [1, 5, 1, 5], [1, 5, 1, 6], [1, 5, 2, 1], [1, 5, 2, 2], [1, 5, 2, 3], [1, 5, 2, 4], [1, 5, 2, 5], [1, 5, 2, 6], [1, 5, 3, 1], [1, 5, 3, 2], [1, 5, 3, 3], [1, 5, 3, 4], [1, 5, 3, 5], [1, 5, 3, 6], [1, 5, 4, 1], [1, 5, 4, 2], [1, 5, 4, 3], [1, 5, 4, 4], [1, 5, 4, 5], [1, 5, 4, 6], [1, 5, 5, 1], [1, 5, 5, 2], [1, 5, 5, 3], [1, 5, 5, 4], [1, 5, 5, 5], [1, 5, 5, 6], [1, 5, 6, 1], [1, 5, 6, 2], [1, 5, 6, 3], [1, 5, 6, 4], [1, 5, 6, 5], [1, 5, 6, 6], [1, 6, 1, 1], [1, 6, 1, 2], [1, 6, 1, 3], [1, 6, 1, 4], [1, 6, 1, 5], [1, 6, 1, 6], [1, 6, 2, 1], [1, 6, 2, 2], [1, 6, 2, 3], [1, 6, 2, 4], [1, 6, 2, 5], [1, 6, 2, 6], [1, 6, 3, 1], [1, 6, 3, 2], [1, 6, 3, 3], [1, 6, 3, 4], [1, 6, 3, 5], [1, 6, 3, 6], [1, 6, 4, 1], [1, 6, 4, 2], [1, 6, 4, 3], [1, 6, 4, 4], [1, 6, 4, 5], [1, 6, 4, 6], [1, 6, 5, 1], [1, 6, 5, 2], [1, 6, 5, 3], [1, 6, 5, 4], [1, 6, 5, 5], [1, 6, 5, 6], [1, 6, 6, 1], [1, 6, 6, 2], [1, 6, 6, 3], [1, 6, 6, 4], [1, 6, 6, 5], [1, 6, 6, 6], [2, 1, 1, 1], [2, 1, 1, 2], [2, 1, 1, 3], [2, 1, 1, 4], [2, 1, 1, 5], [2, 1, 1, 6], [2, 1, 2, 1], [2, 1, 2, 2], [2, 1, 2, 3], [2, 1, 2, 4], [2, 1, 2, 5], [2, 1, 2, 6], [2, 1, 3, 1], [2, 1, 3, 2], [2, 1, 3, 3], [2, 1, 3, 4], [2, 1, 3, 5], [2, 1, 3, 6], [2, 1, 4, 1], [2, 1, 4, 2], [2, 1, 4, 3], [2, 1, 4, 4], [2, 1, 4, 5], [2, 1, 4, 6], [2, 1, 5, 1], [2, 1, 5, 2], [2, 1, 5, 3], [2, 1, 5, 4], [2, 1, 5, 5], [2, 1, 5, 6], [2, 1, 6, 1], [2, 1, 6, 2], [2, 1, 6, 3], [2, 1, 6, 4], [2, 1, 6, 5], [2, 1, 6, 6], [2, 2, 1, 1], [2, 2, 1, 2], [2, 2, 1, 3], [2, 2, 1, 4], [2, 2, 1, 5], [2, 2, 1, 6], [2, 2, 2, 1], [2, 2, 2, 2], [2, 2, 2, 3], [2, 2, 2, 4], [2, 2, 2, 5], [2, 2, 2, 6], [2, 2, 3, 1], [2, 2, 3, 2], [2, 2, 3, 3], [2, 2, 3, 4], [2, 2, 3, 5], [2, 2, 3, 6], [2, 2, 4, 1], [2, 2, 4, 2], [2, 2, 4, 3], [2, 2, 4, 4], [2, 2, 4, 5], [2, 2, 4, 6], [2, 2, 5, 1], [2, 2, 5, 2], [2, 2, 5, 3], [2, 2, 5, 4], [2, 2, 5, 5], [2, 2, 5, 6], [2, 2, 6, 1], [2, 2, 6, 2], [2, 2, 6, 3], [2, 2, 6, 4], [2, 2, 6, 5], [2, 2, 6, 6], [2, 3, 1, 1], [2, 3, 1, 2], [2, 3, 1, 3], [2, 3, 1, 4], [2, 3, 1, 5], [2, 3, 1, 6], [2, 3, 2, 1], [2, 3, 2, 2], [2, 3, 2, 3], [2, 3, 2, 4], [2, 3, 2, 5], [2, 3, 2, 6], [2, 3, 3, 1], [2, 3, 3, 2], [2, 3, 3, 3], [2, 3, 3, 4], [2, 3, 3, 5], [2, 3, 3, 6], [2, 3, 4, 1], [2, 3, 4, 2], [2, 3, 4, 3], [2, 3, 4, 4], [2, 3, 4, 5], [2, 3, 4, 6], [2, 3, 5, 1], [2, 3, 5, 2], [2, 3, 5, 3], [2, 3, 5, 4], [2, 3, 5, 5], [2, 3, 5, 6], [2, 3, 6, 1], [2, 3, 6, 2], [2, 3, 6, 3], [2, 3, 6, 4], [2, 3, 6, 5], [2, 3, 6, 6], [2, 4, 1, 1], [2, 4, 1, 2], [2, 4, 1, 3], [2, 4, 1, 4], [2, 4, 1, 5], [2, 4, 1, 6], [2, 4, 2, 1], [2, 4, 2, 2], [2, 4, 2, 3], [2, 4, 2, 4], [2, 4, 2, 5], [2, 4, 2, 6], [2, 4, 3, 1], [2, 4, 3, 2], [2, 4, 3, 3], [2, 4, 3, 4], [2, 4, 3, 5], [2, 4, 3, 6], [2, 4, 4, 1], [2, 4, 4, 2], [2, 4, 4, 3], [2, 4, 4, 4], [2, 4, 4, 5], [2, 4, 4, 6], [2, 4, 5, 1], [2, 4, 5, 2], [2, 4, 5, 3], [2, 4, 5, 4], [2, 4, 5, 5], [2, 4, 5, 6], [2, 4, 6, 1], [2, 4, 6, 2], [2, 4, 6, 3], [2, 4, 6, 4], [2, 4, 6, 5], [2, 4, 6, 6], [2, 5, 1, 1], [2, 5, 1, 2], [2, 5, 1, 3], [2, 5, 1, 4], [2, 5, 1, 5], [2, 5, 1, 6], [2, 5, 2, 1], [2, 5, 2, 2], [2, 5, 2, 3], [2, 5, 2, 4], [2, 5, 2, 5], [2, 5, 2, 6], [2, 5, 3, 1], [2, 5, 3, 2], [2, 5, 3, 3], [2, 5, 3, 4], [2, 5, 3, 5], [2, 5, 3, 6], [2, 5, 4, 1], [2, 5, 4, 2], [2, 5, 4, 3], [2, 5, 4, 4], [2, 5, 4, 5], [2, 5, 4, 6], [2, 5, 5, 1], [2, 5, 5, 2], [2, 5, 5, 3], [2, 5, 5, 4], [2, 5, 5, 5], [2, 5, 5, 6], [2, 5, 6, 1], [2, 5, 6, 2], [2, 5, 6, 3], [2, 5, 6, 4], [2, 5, 6, 5], [2, 5, 6, 6], [2, 6, 1, 1], [2, 6, 1, 2], [2, 6, 1, 3], [2, 6, 1, 4], [2, 6, 1, 5], [2, 6, 1, 6], [2, 6, 2, 1], [2, 6, 2, 2], [2, 6, 2, 3], [2, 6, 2, 4], [2, 6, 2, 5], [2, 6, 2, 6], [2, 6, 3, 1], [2, 6, 3, 2], [2, 6, 3, 3], [2, 6, 3, 4], [2, 6, 3, 5], [2, 6, 3, 6], [2, 6, 4, 1], [2, 6, 4, 2], [2, 6, 4, 3], [2, 6, 4, 4], [2, 6, 4, 5], [2, 6, 4, 6], [2, 6, 5, 1], [2, 6, 5, 2], [2, 6, 5, 3], [2, 6, 5, 4], [2, 6, 5, 5], [2, 6, 5, 6], [2, 6, 6, 1], [2, 6, 6, 2], [2, 6, 6, 3], [2, 6, 6, 4], [2, 6, 6, 5], [2, 6, 6, 6], [3, 1, 1, 1], [3, 1, 1, 2], [3, 1, 1, 3], [3, 1, 1, 4], [3, 1, 1, 5], [3, 1, 1, 6], [3, 1, 2, 1], [3, 1, 2, 2], [3, 1, 2, 3], [3, 1, 2, 4], [3, 1, 2, 5], [3, 1, 2, 6], [3, 1, 3, 1], [3, 1, 3, 2], [3, 1, 3, 3], [3, 1, 3, 4], [3, 1, 3, 5], [3, 1, 3, 6], [3, 1, 4, 1], [3, 1, 4, 2], [3, 1, 4, 3], [3, 1, 4, 4], [3, 1, 4, 5], [3, 1, 4, 6], [3, 1, 5, 1], [3, 1, 5, 2], [3, 1, 5, 3], [3, 1, 5, 4], [3, 1, 5, 5], [3, 1, 5, 6], [3, 1, 6, 1], [3, 1, 6, 2], [3, 1, 6, 3], [3, 1, 6, 4], [3, 1, 6, 5], [3, 1, 6, 6], [3, 2, 1, 1], [3, 2, 1, 2], [3, 2, 1, 3], [3, 2, 1, 4], [3, 2, 1, 5], [3, 2, 1, 6], [3, 2, 2, 1], [3, 2, 2, 2], [3, 2, 2, 3], [3, 2, 2, 4], [3, 2, 2, 5], [3, 2, 2, 6], [3, 2, 3, 1], [3, 2, 3, 2], [3, 2, 3, 3], [3, 2, 3, 4], [3, 2, 3, 5], [3, 2, 3, 6], [3, 2, 4, 1], [3, 2, 4, 2], [3, 2, 4, 3], [3, 2, 4, 4], [3, 2, 4, 5], [3, 2, 4, 6], [3, 2, 5, 1], [3, 2, 5, 2], [3, 2, 5, 3], [3, 2, 5, 4], [3, 2, 5, 5], [3, 2, 5, 6], [3, 2, 6, 1], [3, 2, 6, 2], [3, 2, 6, 3], [3, 2, 6, 4], [3, 2, 6, 5], [3, 2, 6, 6], [3, 3, 1, 1], [3, 3, 1, 2], [3, 3, 1, 3], [3, 3, 1, 4], [3, 3, 1, 5], [3, 3, 1, 6], [3, 3, 2, 1], [3, 3, 2, 2], [3, 3, 2, 3], [3, 3, 2, 4], [3, 3, 2, 5], [3, 3, 2, 6], [3, 3, 3, 1], [3, 3, 3, 2], [3, 3, 3, 3], [3, 3, 3, 4], [3, 3, 3, 5], [3, 3, 3, 6], [3, 3, 4, 1], [3, 3, 4, 2], [3, 3, 4, 3], [3, 3, 4, 4], [3, 3, 4, 5], [3, 3, 4, 6], [3, 3, 5, 1], [3, 3, 5, 2], [3, 3, 5, 3], [3, 3, 5, 4], [3, 3, 5, 5], [3, 3, 5, 6], [3, 3, 6, 1], [3, 3, 6, 2], [3, 3, 6, 3], [3, 3, 6, 4], [3, 3, 6, 5], [3, 3, 6, 6], [3, 4, 1, 1], [3, 4, 1, 2], [3, 4, 1, 3], [3, 4, 1, 4], [3, 4, 1, 5], [3, 4, 1, 6], [3, 4, 2, 1], [3, 4, 2, 2], [3, 4, 2, 3], [3, 4, 2, 4], [3, 4, 2, 5], [3, 4, 2, 6], [3, 4, 3, 1], [3, 4, 3, 2], [3, 4, 3, 3], [3, 4, 3, 4], [3, 4, 3, 5], [3, 4, 3, 6], [3, 4, 4, 1], [3, 4, 4, 2], [3, 4, 4, 3], [3, 4, 4, 4], [3, 4, 4, 5], [3, 4, 4, 6], [3, 4, 5, 1], [3, 4, 5, 2], [3, 4, 5, 3], [3, 4, 5, 4], [3, 4, 5, 5], [3, 4, 5, 6], [3, 4, 6, 1], [3, 4, 6, 2], [3, 4, 6, 3], [3, 4, 6, 4], [3, 4, 6, 5], [3, 4, 6, 6], [3, 5, 1, 1], [3, 5, 1, 2], [3, 5, 1, 3], [3, 5, 1, 4], [3, 5, 1, 5], [3, 5, 1, 6], [3, 5, 2, 1], [3, 5, 2, 2], [3, 5, 2, 3], [3, 5, 2, 4], [3, 5, 2, 5], [3, 5, 2, 6], [3, 5, 3, 1], [3, 5, 3, 2], [3, 5, 3, 3], [3, 5, 3, 4], [3, 5, 3, 5], [3, 5, 3, 6], [3, 5, 4, 1], [3, 5, 4, 2], [3, 5, 4, 3], [3, 5, 4, 4], [3, 5, 4, 5], [3, 5, 4, 6], [3, 5, 5, 1], [3, 5, 5, 2], [3, 5, 5, 3], [3, 5, 5, 4], [3, 5, 5, 5], [3, 5, 5, 6], [3, 5, 6, 1], [3, 5, 6, 2], [3, 5, 6, 3], [3, 5, 6, 4], [3, 5, 6, 5], [3, 5, 6, 6], [3, 6, 1, 1], [3, 6, 1, 2], [3, 6, 1, 3], [3, 6, 1, 4], [3, 6, 1, 5], [3, 6, 1, 6], [3, 6, 2, 1], [3, 6, 2, 2], [3, 6, 2, 3], [3, 6, 2, 4], [3, 6, 2, 5], [3, 6, 2, 6], [3, 6, 3, 1], [3, 6, 3, 2], [3, 6, 3, 3], [3, 6, 3, 4], [3, 6, 3, 5], [3, 6, 3, 6], [3, 6, 4, 1], [3, 6, 4, 2], [3, 6, 4, 3], [3, 6, 4, 4], [3, 6, 4, 5], [3, 6, 4, 6], [3, 6, 5, 1], [3, 6, 5, 2], [3, 6, 5, 3], [3, 6, 5, 4], [3, 6, 5, 5], [3, 6, 5, 6], [3, 6, 6, 1], [3, 6, 6, 2], [3, 6, 6, 3], [3, 6, 6, 4], [3, 6, 6, 5], [3, 6, 6, 6], [4, 1, 1, 1], [4, 1, 1, 2], [4, 1, 1, 3], [4, 1, 1, 4], [4, 1, 1, 5], [4, 1, 1, 6], [4, 1, 2, 1], [4, 1, 2, 2], [4, 1, 2, 3], [4, 1, 2, 4], [4, 1, 2, 5], [4, 1, 2, 6], [4, 1, 3, 1], [4, 1, 3, 2], [4, 1, 3, 3], [4, 1, 3, 4], [4, 1, 3, 5], [4, 1, 3, 6], [4, 1, 4, 1], [4, 1, 4, 2], [4, 1, 4, 3], [4, 1, 4, 4], [4, 1, 4, 5], [4, 1, 4, 6], [4, 1, 5, 1], [4, 1, 5, 2], [4, 1, 5, 3], [4, 1, 5, 4], [4, 1, 5, 5], [4, 1, 5, 6], [4, 1, 6, 1], [4, 1, 6, 2], [4, 1, 6, 3], [4, 1, 6, 4], [4, 1, 6, 5], [4, 1, 6, 6], [4, 2, 1, 1], [4, 2, 1, 2], [4, 2, 1, 3], [4, 2, 1, 4], [4, 2, 1, 5], [4, 2, 1, 6], [4, 2, 2, 1], [4, 2, 2, 2], [4, 2, 2, 3], [4, 2, 2, 4], [4, 2, 2, 5], [4, 2, 2, 6], [4, 2, 3, 1], [4, 2, 3, 2], [4, 2, 3, 3], [4, 2, 3, 4], [4, 2, 3, 5], [4, 2, 3, 6], [4, 2, 4, 1], [4, 2, 4, 2], [4, 2, 4, 3], [4, 2, 4, 4], [4, 2, 4, 5], [4, 2, 4, 6], [4, 2, 5, 1], [4, 2, 5, 2], [4, 2, 5, 3], [4, 2, 5, 4], [4, 2, 5, 5], [4, 2, 5, 6], [4, 2, 6, 1], [4, 2, 6, 2], [4, 2, 6, 3], [4, 2, 6, 4], [4, 2, 6, 5], [4, 2, 6, 6], [4, 3, 1, 1], [4, 3, 1, 2], [4, 3, 1, 3], [4, 3, 1, 4], [4, 3, 1, 5], [4, 3, 1, 6], [4, 3, 2, 1], [4, 3, 2, 2], [4, 3, 2, 3], [4, 3, 2, 4], [4, 3, 2, 5], [4, 3, 2, 6], [4, 3, 3, 1], [4, 3, 3, 2], [4, 3, 3, 3], [4, 3, 3, 4], [4, 3, 3, 5], [4, 3, 3, 6], [4, 3, 4, 1], [4, 3, 4, 2], [4, 3, 4, 3], [4, 3, 4, 4], [4, 3, 4, 5], [4, 3, 4, 6], [4, 3, 5, 1], [4, 3, 5, 2], [4, 3, 5, 3], [4, 3, 5, 4], [4, 3, 5, 5], [4, 3, 5, 6], [4, 3, 6, 1], [4, 3, 6, 2], [4, 3, 6, 3], [4, 3, 6, 4], [4, 3, 6, 5], [4, 3, 6, 6], [4, 4, 1, 1], [4, 4, 1, 2], [4, 4, 1, 3], [4, 4, 1, 4], [4, 4, 1, 5], [4, 4, 1, 6], [4, 4, 2, 1], [4, 4, 2, 2], [4, 4, 2, 3], [4, 4, 2, 4], [4, 4, 2, 5], [4, 4, 2, 6], [4, 4, 3, 1], [4, 4, 3, 2], [4, 4, 3, 3], [4, 4, 3, 4], [4, 4, 3, 5], [4, 4, 3, 6], [4, 4, 4, 1], [4, 4, 4, 2], [4, 4, 4, 3], [4, 4, 4, 4], [4, 4, 4, 5], [4, 4, 4, 6], [4, 4, 5, 1], [4, 4, 5, 2], [4, 4, 5, 3], [4, 4, 5, 4], [4, 4, 5, 5], [4, 4, 5, 6], [4, 4, 6, 1], [4, 4, 6, 2], [4, 4, 6, 3], [4, 4, 6, 4], [4, 4, 6, 5], [4, 4, 6, 6], [4, 5, 1, 1], [4, 5, 1, 2], [4, 5, 1, 3], [4, 5, 1, 4], [4, 5, 1, 5], [4, 5, 1, 6], [4, 5, 2, 1], [4, 5, 2, 2], [4, 5, 2, 3], [4, 5, 2, 4], [4, 5, 2, 5], [4, 5, 2, 6], [4, 5, 3, 1], [4, 5, 3, 2], [4, 5, 3, 3], [4, 5, 3, 4], [4, 5, 3, 5], [4, 5, 3, 6], [4, 5, 4, 1], [4, 5, 4, 2], [4, 5, 4, 3], [4, 5, 4, 4], [4, 5, 4, 5], [4, 5, 4, 6], [4, 5, 5, 1], [4, 5, 5, 2], [4, 5, 5, 3], [4, 5, 5, 4], [4, 5, 5, 5], [4, 5, 5, 6], [4, 5, 6, 1], [4, 5, 6, 2], [4, 5, 6, 3], [4, 5, 6, 4], [4, 5, 6, 5], [4, 5, 6, 6], [4, 6, 1, 1], [4, 6, 1, 2], [4, 6, 1, 3], [4, 6, 1, 4], [4, 6, 1, 5], [4, 6, 1, 6], [4, 6, 2, 1], [4, 6, 2, 2], [4, 6, 2, 3], [4, 6, 2, 4], [4, 6, 2, 5], [4, 6, 2, 6], [4, 6, 3, 1], [4, 6, 3, 2], [4, 6, 3, 3], [4, 6, 3, 4], [4, 6, 3, 5], [4, 6, 3, 6], [4, 6, 4, 1], [4, 6, 4, 2], [4, 6, 4, 3], [4, 6, 4, 4], [4, 6, 4, 5], [4, 6, 4, 6], [4, 6, 5, 1], [4, 6, 5, 2], [4, 6, 5, 3], [4, 6, 5, 4], [4, 6, 5, 5], [4, 6, 5, 6], [4, 6, 6, 1], [4, 6, 6, 2], [4, 6, 6, 3], [4, 6, 6, 4], [4, 6, 6, 5], [4, 6, 6, 6], [5, 1, 1, 1], [5, 1, 1, 2], [5, 1, 1, 3], [5, 1, 1, 4], [5, 1, 1, 5], [5, 1, 1, 6], [5, 1, 2, 1], [5, 1, 2, 2], [5, 1, 2, 3], [5, 1, 2, 4], [5, 1, 2, 5], [5, 1, 2, 6], [5, 1, 3, 1], [5, 1, 3, 2], [5, 1, 3, 3], [5, 1, 3, 4], [5, 1, 3, 5], [5, 1, 3, 6], [5, 1, 4, 1], [5, 1, 4, 2], [5, 1, 4, 3], [5, 1, 4, 4], [5, 1, 4, 5], [5, 1, 4, 6], [5, 1, 5, 1], [5, 1, 5, 2], [5, 1, 5, 3], [5, 1, 5, 4], [5, 1, 5, 5], [5, 1, 5, 6], [5, 1, 6, 1], [5, 1, 6, 2], [5, 1, 6, 3], [5, 1, 6, 4], [5, 1, 6, 5], [5, 1, 6, 6], [5, 2, 1, 1], [5, 2, 1, 2], [5, 2, 1, 3], [5, 2, 1, 4], [5, 2, 1, 5], [5, 2, 1, 6], [5, 2, 2, 1], [5, 2, 2, 2], [5, 2, 2, 3], [5, 2, 2, 4], [5, 2, 2, 5], [5, 2, 2, 6], [5, 2, 3, 1], [5, 2, 3, 2], [5, 2, 3, 3], [5, 2, 3, 4], [5, 2, 3, 5], [5, 2, 3, 6], [5, 2, 4, 1], [5, 2, 4, 2], [5, 2, 4, 3], [5, 2, 4, 4], [5, 2, 4, 5], [5, 2, 4, 6], [5, 2, 5, 1], [5, 2, 5, 2], [5, 2, 5, 3], [5, 2, 5, 4], [5, 2, 5, 5], [5, 2, 5, 6], [5, 2, 6, 1], [5, 2, 6, 2], [5, 2, 6, 3], [5, 2, 6, 4], [5, 2, 6, 5], [5, 2, 6, 6], [5, 3, 1, 1], [5, 3, 1, 2], [5, 3, 1, 3], [5, 3, 1, 4], [5, 3, 1, 5], [5, 3, 1, 6], [5, 3, 2, 1], [5, 3, 2, 2], [5, 3, 2, 3], [5, 3, 2, 4], [5, 3, 2, 5], [5, 3, 2, 6], [5, 3, 3, 1], [5, 3, 3, 2], [5, 3, 3, 3], [5, 3, 3, 4], [5, 3, 3, 5], [5, 3, 3, 6], [5, 3, 4, 1], [5, 3, 4, 2], [5, 3, 4, 3], [5, 3, 4, 4], [5, 3, 4, 5], [5, 3, 4, 6], [5, 3, 5, 1], [5, 3, 5, 2], [5, 3, 5, 3], [5, 3, 5, 4], [5, 3, 5, 5], [5, 3, 5, 6], [5, 3, 6, 1], [5, 3, 6, 2], [5, 3, 6, 3], [5, 3, 6, 4], [5, 3, 6, 5], [5, 3, 6, 6], [5, 4, 1, 1], [5, 4, 1, 2], [5, 4, 1, 3], [5, 4, 1, 4], [5, 4, 1, 5], [5, 4, 1, 6], [5, 4, 2, 1], [5, 4, 2, 2], [5, 4, 2, 3], [5, 4, 2, 4], [5, 4, 2, 5], [5, 4, 2, 6], [5, 4, 3, 1], [5, 4, 3, 2], [5, 4, 3, 3], [5, 4, 3, 4], [5, 4, 3, 5], [5, 4, 3, 6], [5, 4, 4, 1], [5, 4, 4, 2], [5, 4, 4, 3], [5, 4, 4, 4], [5, 4, 4, 5], [5, 4, 4, 6], [5, 4, 5, 1], [5, 4, 5, 2], [5, 4, 5, 3], [5, 4, 5, 4], ...]
gen
<generator object <genexpr> at 0x7fe6b2685cf0>
Példa: Írjunk egy teljes programot, az előző kódrészleteket egy-egy függvénybe téve, majd meghívva a main()
függvényből! Mentsük el e kódot egy külön fájlba, és futassuk terminálból!
from random import randint
def simulate(n):
count = 0
for _ in range(n):
seq = [randint(1,6) for i in range(4)]
if 6 in seq:
count += 1
return count/n
def probability():
gen = ([i1, i2, i3, i4] for i1 in range(1,7) for i2 in range(1,7) \
for i3 in range(1,7) for i4 in range(1,7))
count = 0
for i in gen:
if 6 in i:
count += 1
return count/6**4
def main():
print(f" Nyerés")
print(f"Relatív gyakoriság 100 kísérletből {simulate(100):.5f}")
print(f"Relatív gyakoriság 10000 kísérletből {simulate(10000):.5f}")
print(f"Valószínűség {probability():.5f}")
if __name__ == "__main__":
main()
Nyerés Relatív gyakoriság 100 kísérletből 0.59000 Relatív gyakoriság 10000 kísérletből 0.51470 Valószínűség 0.51775
A valószínűség e feladatban az összes eset felsorolása nélkül is meghatározható egy kis matekkal:
1-(5/6)**4
0.5177469135802468
A programozási tipp: Több egymásba ágyazott lista eredménye megkapható az itertools
modul product
függvényével. Pl. a 4 kockadobás összes lehetséges esete listába tehető a következő kóddal:
from itertools import product
dice = range(1, 7)
four = product(dice, dice, dice, dice)
for _ in four:
print(_)
(1, 1, 1, 1) (1, 1, 1, 2) (1, 1, 1, 3) (1, 1, 1, 4) (1, 1, 1, 5) (1, 1, 1, 6) (1, 1, 2, 1) (1, 1, 2, 2) (1, 1, 2, 3) (1, 1, 2, 4) (1, 1, 2, 5) (1, 1, 2, 6) (1, 1, 3, 1) (1, 1, 3, 2) (1, 1, 3, 3) (1, 1, 3, 4) (1, 1, 3, 5) (1, 1, 3, 6) (1, 1, 4, 1) (1, 1, 4, 2) (1, 1, 4, 3) (1, 1, 4, 4) (1, 1, 4, 5) (1, 1, 4, 6) (1, 1, 5, 1) (1, 1, 5, 2) (1, 1, 5, 3) (1, 1, 5, 4) (1, 1, 5, 5) (1, 1, 5, 6) (1, 1, 6, 1) (1, 1, 6, 2) (1, 1, 6, 3) (1, 1, 6, 4) (1, 1, 6, 5) (1, 1, 6, 6) (1, 2, 1, 1) (1, 2, 1, 2) (1, 2, 1, 3) (1, 2, 1, 4) (1, 2, 1, 5) (1, 2, 1, 6) (1, 2, 2, 1) (1, 2, 2, 2) (1, 2, 2, 3) (1, 2, 2, 4) (1, 2, 2, 5) (1, 2, 2, 6) (1, 2, 3, 1) (1, 2, 3, 2) (1, 2, 3, 3) (1, 2, 3, 4) (1, 2, 3, 5) (1, 2, 3, 6) (1, 2, 4, 1) (1,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) (6, 5, 5, 3) (6, 5, 5, 4) (6, 5, 5, 5) (6, 5, 5, 6) (6, 5, 6, 1) (6, 5, 6, 2) (6, 5, 6, 3) (6, 5, 6, 4) (6, 5, 6, 5) (6, 5, 6, 6) (6, 6, 1, 1) (6, 6, 1, 2) (6, 6, 1, 3) (6, 6, 1, 4) (6, 6, 1, 5) (6, 6, 1, 6) (6, 6, 2, 1) (6, 6, 2, 2) (6, 6, 2, 3) (6, 6, 2, 4) (6, 6, 2, 5) (6, 6, 2, 6) (6, 6, 3, 1) (6, 6, 3, 2) (6, 6, 3, 3) (6, 6, 3, 4) (6, 6, 3, 5) (6, 6, 3, 6) (6, 6, 4, 1) (6, 6, 4, 2) (6, 6, 4, 3) (6, 6, 4, 4) (6, 6, 4, 5) (6, 6, 4, 6) (6, 6, 5, 1) (6, 6, 5, 2) (6, 6, 5, 3) (6, 6, 5, 4) (6, 6, 5, 5) (6, 6, 5, 6) (6, 6, 6, 1) (6, 6, 6, 2) (6, 6, 6, 3) (6, 6, 6, 4) (6, 6, 6, 5) (6, 6, 6, 6)
n = 1000
count = 0
for _ in range(n):
for __ in range(24):
seq = [randint(1,6), randint(1,6)]
if [6,6] == seq:
count += 1
break
print(count/n)
0.47
6**4, 36**24
(1296, 22452257707354557240087211123792674816)
Az utóbbi olyan hatalmas szám, hogy 24-szer 2 kockadobás lehetséges kimeneteleinek felsorolása lehetetlen. Itt segíthet a matematika:
1-(35/36)**24
0.4914038761309034
Gyakorló feladat: Írjunk a fentihez hasonló teljes programot a 24-szer feldobott 2 kocka esetére!
print("The value of __name__ is:", repr(__name__))
The value of __name__ is: '__main__'
Az általános sablon a feladatok (így ennek a feladatnak a) megoldásához:
def test():
"""
Add meg a függvény leírását!
"""
print("Ez egy függvény!")
def main():
print("Itt meghívjuk a függvényt!")
test()
if __name__ == "__main__":
# csak akkor hajtódik végre, ha szkriptként futtatjuk!
main()
Itt meghívjuk a függvényt! Ez egy függvény!
A Rizikó nevű társasjátékban egyszerre egy játékos maximum három katonájával tud támadni, míg a védekező játékos legföljebb kettővel tud védekezni. Pontosan három támadó és két védő esetén az összecsapás a következőképpen zajlik. A támadó játékos három piros kockával míg a védekező játékos két kék kockával dob. Ezután összehasonlítjuk a támadó és a védő legnagyobb dobását. A kisebb érték birtokosa veszít egy katonát, a döntetlen a védőnek kedvez, vagyis ekkor a támadó veszít egyet. Ezután a két fél második legnagyobb dobását is összehasonlítjuk ugyanilyen módon. Így az összecsapásnak három kimenetele lehet: a támadó veszít két katonát, döntetlen, azaz mindkét fél veszít 1-1 katonát, a védő veszít két katonát.
Számolja ki a három kimenetel pontos valószínűségét az összes lehetséges eset megvizsgálásával. A valószínűséget a kedvező esetek és az összes esetek hányadosa adja. Ezeket az eredményeket is 5 tizedes pontossággal írja ki, köztük 3 szóközzel! A program kimenete így nézzen ki (természetesen más számadatokkal)
Támadó Döntetlen Védő
1000 kísérlet: 0.35200 0.44400 0.20400
1000000 kísérlet: 0.33988 0.43011 0.23001
Valószínűség: 0.34000 0.43000 0.23000
A feladat célja: ismerkedés a klasszikus valószínűségi mezővel, a relatív gyakoriság és a valószínűség fogalmával és az elméleti valószínűség kísérleti megközelítésével.
random
és itertools
modulok egy-egy függvényének megismerése.A teamsben feltöltendő megoldás egy .py kiterjesztésű szövegfájl legyen, azaz egy parancssorból futtatható Python3 kód. A kód elején következő szöveg álljon:
"""
E programot magam kódoltam, nem másoltam vagy
írtam át más kódját, és nem adtam át másnak.
Aláírás Teljes Névvel
"""