Last active
August 29, 2015 14:26
-
-
Save msimonin/a9e9390cc21832b860da to your computer and use it in GitHub Desktop.
king of tokyo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
6 dés, 3 relances. | |
Quelle est la probabilité d'obtenir au moins 3 faces identiques. | |
choix : | |
* on décide a priori d'essayer dobtenir au moins 3 dés avec une face prédéfinie (par exemple 1) | |
* on décide après le premier lancer d'obtenir au moins 3 dés avec une face qui a déjà été obtenue au premier lancer | |
""" | |
from random import randint | |
def relance(x, match): | |
""" | |
relance le dé si son résultat ne correspond pas à *match* | |
x : le résultat du dé | |
match : résultat attendu | |
""" | |
if x != match: | |
return randint(1,6) | |
return x | |
def metarelance(match): | |
""" | |
pour un match donné renvoie la fonction qui a x associe relance(x, match) | |
(utile pour utiliser la fonction map) | |
""" | |
return lambda x: relance(x, match) | |
# initialisation | |
# nombre de dés | |
des = 6 | |
# nombre de relance autorisées | |
retries = 3 | |
# compte les résultats positifes (au moins 3 faces identiques) | |
positive = 0 | |
# nombre total de simulation | |
total = 0 | |
# nombre de simulations | |
N = 1000000 | |
for i in range(N): | |
a = [0 for i in range(des)] | |
total = total + 1 | |
# on decide du dé à garder après le 1er lancer | |
a = map(metarelance(1), a) | |
match = a[0] | |
""" | |
variante : on décide a priori | |
match = 1 | |
for i in range(retries): | |
""" | |
for i in range(retries - 1 ): | |
a = map(metarelance(match), a) | |
# on compte les dés égaux à *match* | |
c = sum(x == match for x in a) | |
if (c >= 3): | |
positive = positive + 1 | |
print("{} - Total = {}, Positive = {} ".format(positive / float(total), total, positive)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment