Skip to content

Instantly share code, notes, and snippets.

@halexus
Created April 7, 2019 16:33
Show Gist options
  • Save halexus/9cfb06c8ef94216c1ed74a42012c039d to your computer and use it in GitHub Desktop.
Save halexus/9cfb06c8ef94216c1ed74a42012c039d to your computer and use it in GitHub Desktop.
Aufgabe 24 UE Wahrscheinlichkeitstheorie
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Aufgabe 24\n",
"\n",
"Bei einem Glücksspiel wird eine faire Münze so lange geworfen, bis zum ersten Mal \"Zahl\" fällt: Dies beendet das Spiel. Der Gewinn richtet sich nach der Anzahl der Münzwürfe. War es nur einer, dann erhält der Spieler 1 Euro. Bei\n",
"zwei Würfen (also einmal \"Kopf\", einmal \"Zahl\") erhält er 2 Euro, bei drei Würfen 3 Euro, bei vier Würfen 4 Euro, und so weiter.\n",
"\n",
"Für die Teilnahme an dem Glücksspiel müssen 2 Euro bezahlt werden.\n",
"Ist das Spiel fair?\n",
"***"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"import statistics"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Durchschnittlicher Gewinn: 0.0002510000000000012 €\n"
]
}
],
"source": [
"def simulation(N = 1000000):\n",
" '''Simuliere welcher Betrag durchschnittlich bei N Runden ausbezahlt wird.'''\n",
" muenze = ('K', 'Z') # Zweiseitige Münze mit 'K'opf und 'Z'ahl\n",
" gewinne = [] # Hier werden die entsprechenden Gewinne aller Runden eingetragen\n",
" for _ in range(N): # Simuliere die N Runden\n",
" gewinn = 1\n",
" while random.choice(muenze) == 'K':\n",
" gewinn += 1\n",
" gewinne.append(gewinn)\n",
" return statistics.mean(gewinne)\n",
"\n",
"print(\"Durchschnittlicher Gewinn:\", simulation()-2, \"€\") # -2 da 2 Euro bezahlt werden müssen"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment