Last active
January 12, 2021 10:54
-
-
Save jul/009cc53374fdbda1c1fdd2d60381ac56 to your computer and use it in GitHub Desktop.
simulation de 100 tirages au hasard et mesure des fréquences des gains a Végas
This file contains 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
#!/usr/bin/env python3 | |
from archery import mdict | |
from functools import reduce | |
from random import randint, seed | |
from json import dumps | |
from sys import argv | |
pp = lambda d:print(dumps(d, indent=4)) | |
_seed = argv[1] | |
print(_seed) | |
seed(int(_seed)) | |
proba = { | |
160000 : 3, | |
200000 : 6, | |
100000 : 9, | |
50000 : 15, | |
14400 : 50, | |
300 : 100, | |
20 : 1000, | |
2 : 50000, | |
} | |
rank = mdict() | |
cursor=0 | |
for k,v in proba.items(): | |
rank += mdict({ v : (cursor, cursor + k) }) | |
cursor+=k | |
max_rank_w_gain = cursor | |
#pp( rank ) | |
total_gain = gain = 0 | |
in_ = lambda v, _range : _range[0] <= v < _range[1] | |
frequence = mdict() | |
NB_TEST=250000 | |
MEDIAN=NB_TEST/2 | |
for test in range(NB_TEST): | |
gain = 0 | |
for i in range(100): | |
gain-=3 | |
randme = randint(0,2000000-1) | |
if randme > max_rank_w_gain: | |
#print("lose") | |
continue | |
#print(randme) | |
for g, between in rank.items(): | |
if in_( randme, between): | |
#print( between ) | |
#print( randme ) | |
gain += g | |
#print(gain) | |
continue | |
total_gain += gain | |
frequence += mdict({ gain : 1 }) | |
#pp(frequence) | |
gagne, perd = 0,0 | |
for k, v in frequence.items(): | |
if k > 0: | |
gagne+=v | |
else: | |
perd += v | |
print("perd %d / gain %d" %(perd, gagne)) | |
print("proba gain = %f%%" % (gagne / (perd + gagne) * 100)) | |
print("gain moyen = %f€" % (total_gain / NB_TEST)) | |
sf = list(sorted( frequence.keys() )) | |
histo = tuple() | |
RANGTILE=31 | |
pas = int(len(sf) /RANGTILE) | |
print('PAS %d' % pas) | |
check=0 | |
first=True | |
for i in range(len(sf)-1, 0, -1 * pas): | |
haut = i | |
bas = i - pas+1 | |
print("[%d," % haut, end='') | |
if bas < pas : | |
bas = 0 | |
print('%d]' % bas) | |
if haut < pas-1 or bas < 0: | |
print("obO? last range is skipped ") | |
break | |
acc= sum([ frequence[sf[j]] for j in range(bas-1, haut)] ) | |
histo += (( | |
sf[bas], | |
sf[haut], | |
acc/100, | |
),) | |
check+=acc | |
if check >= MEDIAN and first: | |
print("*****la mediane est entre [%d, %d]" % (sf[haut],sf[bas])) | |
first=False | |
assert check == NB_TEST | |
print("test of conservation is passed") | |
mmax = reduce(max, [ t[2] for t in histo ]) | |
for t in histo: | |
ht, bas= t[:2] | |
print("[%5d€;%5d€]" % (bas, ht), end='' ) | |
print( (int(t[2]*110.0/mmax) * "*")) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
test "property based" l92 chaque utilisation du code est aussi un test.
La graîne permet d'assurer la reproducibilité du hasard est pouvoir déboguer.
Le code avec du hasard, c'est un autre monde.