Python :: Aufgabe #178
4 Lösungen
kleine simulation zweier zufallsereignisse
Anfänger - Python
von hollst
- 21.03.2018 um 21:25 Uhr
Gegeben seien zwei unabhängige Zufallsereignisse A und B, wobei A mit einer
Wahrscheinlichkeit von 70 % eintritt und B mit eine Wahrescheinlichkeit von 10 %.
Durch (MontoCarlo-) Simulation schätze man ab, mit welchen Wahrscheinlichkeiten das Doppelereignis (A, B)
vorkommt für die Fälle
a) weder A noch B sind eingetreten,
b) entweder A oder B (oder beide) ist (sind) eingetreten.
Man kann das natürlich auch ausrechnen, aber grau ist bekanntlich alle Theorie.
Wahrscheinlichkeit von 70 % eintritt und B mit eine Wahrescheinlichkeit von 10 %.
Durch (MontoCarlo-) Simulation schätze man ab, mit welchen Wahrscheinlichkeiten das Doppelereignis (A, B)
vorkommt für die Fälle
a) weder A noch B sind eingetreten,
b) entweder A oder B (oder beide) ist (sind) eingetreten.
Man kann das natürlich auch ausrechnen, aber grau ist bekanntlich alle Theorie.
Lösungen:
import random
random.seed()
a, b = 0, 0
count = 1
while count <= 10000000:
iA = random.randint(1,10)
iB = random.randint(1,10)
if (iA > 7 and iB > 1):
a += 1
if (iA <= 7 or iB == 1):
b += 1
count += 1
print("Wahrscheinlichkeit für Ereignis a:", str(100*float(a/count)) + "%")
print("Wahrscheinlichkeit für Ereignis b:", str(100*float(b/count)) + "%")
import random
number_runs = 100000
count_first = 0
count_second = 0
for _ in range(number_runs):
a,b = random.randint(0,9),random.randint(0,9)
# check first:
# A tritt ein, falls a < 7, b tritt ein falls b = 9
# funktion oben formatiert gleichverteilung auf [0,9] auf das gewünschte
if a >= 7 and b != 9:
count_first = count_first + 1
if a < 7 or b == 9:
count_second = count_second +1
print('beide nicht erfolgreich: %s' % str(count_first/float(number_runs)) )
print('mindestens eins erfolgreich: %s' % str(count_second/float(number_runs)) )
#Python3.7
# -*- coding: iso-8859-15 -*-
import random
random.seed()
testanzahl=100000
AoB=0 #Zähler für den Fall A_oder_B
AuB=0 #Zähler für den Fall A_und_B
Aa=0 #Zähler für den Fall A
Ba=0 #Zähler für den Fall B
ABnot=0 #Zähler für weder A noch B
for i in range(testanzahl):
a = random.randint(0,9)
b = random.randint(0,9)
if a<7:
at=True
Aa+=1
else:at=False
if b==9:
bt=True
Ba+=1
else:bt=False
if at or bt:AoB+=1
if at and bt:AuB+=1
if not at and not bt:ABnot+=1
print("Bei {} Versuchen trat der Fall A {} mal, der Fall B {} mal und keines von beiden {} mal ein.".format(testanzahl,Aa,Ba,ABnot))
print("Der Fall A_oder_B trat dabei {} mal und der Fall A_und_B {} mal auf.".format(AoB,AuB))
print()
Aap=round(100/testanzahl*Aa,2)
Bap=round(100/testanzahl*Ba,2)
AoBp=round(100/testanzahl*AoB,2)
AuBp=round(100/testanzahl*AuB,2)
ABnotp=round(100/testanzahl*ABnot,2)
print("Bei {} Versuchen trat der Fall A zu {}%, der Fall B zu {}% und keines von beiden zu {}% ein.".format(testanzahl,Aap,Bap,ABnotp))
print("Der Fall A_oder_B ist dabei zu {}% und der Fall A_und_B zu {}% eingetreten.".format(AoBp,AuBp))
import random
versuche = int(input("Wie oft soll das Experiment durchgeführt werden? >"))
a = 0
for würfeln in range(0,versuche):
Ereig_A = random.randint(1,10)
Ereig_B = random.randint(1,10)
if Ereig_A >7 and Ereig_B >1: #A tritt nicht ein und B tritt nicht ein
a += 1
print(f"Die Wahrscheinlichkeit, dass A und B nicht eintreten ist: {round(a/versuche*100,2)} %")
print(f"Die Wahrscheinlichkeit, dass entweder A oder B (oder beide) ieingetreten sind ist {100-round(a/versuche*100,2)} %")