mot = 'BONJOUR' def h1(ch): return ord(ch[0])-ord('A') def h2(ch): decalage = ord('A') res = 0 for car in ch : res = res + ord(car) - decalage return res def h3(ch): decalage = ord('A') res = 0 for i in range(len(ch)) : res = res + (ord(ch[i]) - decalage)*26**i return res import numpy as np def h4(ch): x = 2166136261 prime = 16777619 for car in ch : x = x^(ord(car)-ord('A')) x = (x * prime) x = x % (2**32) return x fid = open(r'C:\Users\bapt-\Desktop\BOULOT\info_22\TD\Dictionnaires\mots.txt') lst_mots = fid.readlines() fid.close() import random as rd def tirage_mot(lst): n = len(lst) i = rd.randint(0, n-1) return lst[i].strip('\n') N = 1000000 L = [0 for i in range(4)] n = 10000 for i in range(N): mot1 = tirage_mot(lst_mots) mot2 = tirage_mot(lst_mots) if h1(mot1)%n == h1(mot2)%n : L[0] = L[0] + 1/N if h2(mot1)%n == h2(mot2)%n : L[1] = L[1] + 1/N if h3(mot1)%n == h3(mot2)%n : L[2] = L[2] + 1/N if h4(mot1)%n == h4(mot2)%n : L[3] = L[3] + 1/N print(L)