import matplotlib.pyplot as plt from random import * def traversee_une_fois() : # Réglages d'appoint de la fenêtre graphique ax = plt.gca() ax.spines["bottom"].set_position("zero") plt.grid(True) plt.plot([0, 10], [-2, -2], "-b") # Trace des plt.plot([0, 10], [2,2], "-b") # bords du pont plt.axis([0, 10, -3, 3]) # Initialisation des listes # des abscisses et ordonnées du marcheur X = [0] Y = [0] while X[-1] < 10 and abs(Y[-1]) <= 2 : de = randint(0,2) X.append(X[-1] + 1) Y.append(Y[-1] + de - 1) plt.plot(X, Y, "-or") plt.show() if abs(Y[-1]) <= 2 and X[-1] == 10 : s = 1 else : s = 0 return s def traversee_une_fois_sans_graphe() : X = [0] Y = [0] while X[-1] < 10 and abs(Y[-1]) <= 2 : de = randint(0, 2) X.append(X[-1] + 1) Y.append(Y[-1] + de - 1) if abs(Y[-1]) <= 2 and X[-1] == 10 : s = 1 else : s = 0 return s def traversee_multiple(n): # n traversées effectif = 0 for compteur in range(n) : if traversee_une_fois_sans_graphe() == 1 : effectif = effectif + 1 return effectif / n def explore_chemin(n, y) : r = 0 if n == 0 : if abs(y) <= 2 : r = 1 else : r = 0 else : if abs(y) <= 2 : for p in [-1, 0, 1] : r = r + explore_chemin(n - 1, y + p) return r