{"id":2031,"date":"2020-02-15T16:37:25","date_gmt":"2020-02-15T15:37:25","guid":{"rendered":"https:\/\/www.mathweb.fr\/euclide\/?p=2031"},"modified":"2022-01-27T17:35:36","modified_gmt":"2022-01-27T16:35:36","slug":"marche-aleatoire-et-python","status":"publish","type":"post","link":"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/","title":{"rendered":"Marche al\u00e9atoire en Python"},"content":{"rendered":"\n<p>Marche al\u00e9atoire en Python: c&#8217;est le th\u00e8me d&#8217;un exercices que j&#8217;ai vu dans un livre de sp\u00e9cialit\u00e9 Math niveau 1\u00e8re. Il concerne la <a href=\"https:\/\/fr.wikipedia.org\/wiki\/Marche_al%C3%A9atoire\" target=\"_blank\" rel=\"noreferrer noopener\">marche al\u00e9atoire<\/a> d&#8217;une puce dans un plan rapport\u00e9 \u00e0 un rep\u00e8re orthonorm\u00e9.<\/p>\n\n\n\n<!--more Lire la suite-->\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-white ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Au menu sur cette page...<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/#Marche_aleatoire_en_Python_le_contexte\" >Marche al\u00e9atoire en Python: le contexte<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/#Marche_aleatoire_le_programme_Python\" >Marche al\u00e9atoire: le programme Python<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/#Une_fonction_qui_simule_une_marche_aleatoire_en_Python\" >Une fonction qui simule une marche al\u00e9atoire en Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/#Une_fonction_qui_calcule_une_frequence\" >Une fonction qui calcule une fr\u00e9quence<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/#Etude_mathematique_dune_marche_aleatoire\" >\u00c9tude math\u00e9matique d&#8217;une marche al\u00e9atoire<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/#Simplifions_un_peu%E2%80%A6\" >Simplifions un peu&#8230;<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/#Une_marche_a_4_pas%E2%80%A6\" >Une marche \u00e0 4 pas&#8230;<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/#Une_marche_a_10_pas\" >Une marche \u00e0 10 pas<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/#Avec_aucun_%E2%80%9CN%E2%80%9D\" >Avec aucun &#8220;N&#8221;<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/#Avec_un_%E2%80%9CN%E2%80%9D\" >Avec un &#8220;N&#8221;<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/#Avec_deux_%E2%80%9CN%E2%80%9D\" >Avec deux &#8220;N&#8221;<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/#Avec_k_%E2%80%9CN%E2%80%9D_0_leq_k_leq_5\" >Avec k &#8220;N&#8221;, \\(0 \\leq k \\leq 5\\)<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"marche-aleatoire-en-python-le-contexte\"><span class=\"ez-toc-section\" id=\"Marche_aleatoire_en_Python_le_contexte\"><\/span>Marche al\u00e9atoire en Python: le contexte<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Une puce part de l&#8217;origine du rep\u00e8re et effectue 10 pas de fa\u00e7on al\u00e9atoire : \u00e0 chaque pas, elle peut avancer ou reculer en abscisses ou en ordonn\u00e9es. Voici un exemple de cette marche:<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"553\" height=\"471\" src=\"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2020\/02\/marche-aleatoire.png\" alt=\"marche al\u00e9atoire python\" class=\"wp-image-2032\" srcset=\"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2020\/02\/marche-aleatoire.png 553w, https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2020\/02\/marche-aleatoire-300x256.png 300w\" sizes=\"auto, (max-width: 553px) 100vw, 553px\" \/><figcaption>Exemple de marche al\u00e9atoire (&#8220;D&#8221; pour d\u00e9part et &#8220;A&#8221; pour arriv\u00e9e)<\/figcaption><\/figure><\/div>\n\n\n\n<p>On s&#8217;int\u00e9resse \u00e0 la probabilit\u00e9 de revenir \u00e0 l&#8217;origine du rep\u00e8re \u00e0 la fin de la marche. Pour cela, on va utiliser un programme Python&#8230;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"marche-aleatoire-le-programme-python\"><span class=\"ez-toc-section\" id=\"Marche_aleatoire_le_programme_Python\"><\/span>Marche al\u00e9atoire: le programme Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"une-fonction-qui-simule-une-marche-aleatoire-en-python\"><span class=\"ez-toc-section\" id=\"Une_fonction_qui_simule_une_marche_aleatoire_en_Python\"><\/span>Une fonction qui simule une marche al\u00e9atoire en Python<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Commen\u00e7ons en effet par impl\u00e9menter une fonction qui a pour mission de retourner les coordonn\u00e9es du point d&#8217;arriv\u00e9e \u00e0 l&#8217;issue de la marche:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"false\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Python &lt; 3.10\nfrom random import randint\n\ndef marche(n):\n    x, y = 0, 0\n    for i in range(n):\n        b = randint(0,3)\n        if b == 0:\n            x += 1\n        elif b == 1:\n            x -= 1\n        elif b == 2:\n            y += 1\n        else:\n            y -= 1\n    \n    return (x,y)<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"false\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Python 3.10+\nfrom random import randint\n\ndef marche(n):\n    x, y = 0, 0\n    for i in range(n):\n        match randint(0,3):\n            case 0: x += 1\n            case 1: x -= 1\n            case 2: y += 1\n            case 3: y -= 1\n    \n    return (x,y)\n<\/pre>\n\n\n\n<p>On commence par importer la fonction randint(a,b)  qui choisit al\u00e9atoirement un nombre entier entre a et b (compris), car nous avons besoin de cette fonction dans celle que nous allons \u00e9crire.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-style-default is-layout-flow wp-block-quote-is-layout-flow\"><p>Remarque :  encore une fois (oui, je ragote), inutile d&#8217;importer TOUTES les fonctions de random&#8230; Donc \u00e9vitez le &#8220;from random import *&#8221; ou pire encore : &#8220;import random&#8221;.<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"une-fonction-qui-calcule-une-frequence\"><span class=\"ez-toc-section\" id=\"Une_fonction_qui_calcule_une_frequence\"><\/span>Une fonction qui calcule une fr\u00e9quence<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Maintenant, impl\u00e9mentons une fonction <strong>cible(x,y,n)<\/strong> qui retournera la fr\u00e9quence de fois o\u00f9 l&#8217;on est arriv\u00e9 \u00e0 l&#8217;origine \u00e0 la fin de la marche sur les <em>n<\/em> marches effectu\u00e9es.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"false\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def cible(x,y,n):\n    c = 0 # compteur de fois o\u00f9 l'on atteint (0,0) \u00e0 l'issue d'une marche\n    for i in range(n):\n        if marche(10) == (x,y):\n            c += 1\n\n    return c \/ n<\/pre>\n\n\n\n<p>Maintenant, regardons ce que cela donne avec <em>n<\/em> = 1000:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"false\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">print( cible(0,0,1000) )<\/pre>\n\n\n\n<p>J&#8217;obtiens 0,081. Comme je suis de nature sceptique (comme la fosse), je d\u00e9cide de faire 20 simulations:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"false\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for i in range(20):\n    print( cible(0,0,1000) )<\/pre>\n\n\n\n<p>Et l\u00e0, j&#8217;obtiens:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">0.042\n0.058\n0.053\n0.066\n0.058\n0.059\n0.055\n0.059\n0.063\n0.062\n0.058\n0.06\n0.062\n0.075\n0.05\n0.051\n0.056\n0.068\n0.053\n0.048<\/pre>\n\n\n\n<p>La premi\u00e8re valeur \u00e9tait donc une valeur peut repr\u00e9sentative au final&#8230; Ces probabilit\u00e9s fluctuent autour de 0,0625 qui n&#8217;est autre que \\(0,25^2\\). Essayons d&#8217;expliquer cela&#8230;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"etude-mathematique-d-une-marche-aleatoire\"><span class=\"ez-toc-section\" id=\"Etude_mathematique_dune_marche_aleatoire\"><\/span>\u00c9tude math\u00e9matique d&#8217;une marche al\u00e9atoire<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"simplifions-un-peu\"><span class=\"ez-toc-section\" id=\"Simplifions_un_peu%E2%80%A6\"><\/span>Simplifions un peu&#8230;<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Plut\u00f4t que de prendre 10 pas pour une marche, choisissons-en 2, et ce pour pouvoir facilement repr\u00e9senter cette exp\u00e9rience al\u00e9atoire \u00e0 l&#8217;aide d&#8217;un arbre:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"205\" height=\"736\" src=\"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2020\/02\/marche-aleatoire-arbre.png\" alt=\"\" class=\"wp-image-2033\"\/><figcaption>Arbre de probabilit\u00e9s d&#8217;une marche al\u00e9atoire de 2 pas<\/figcaption><\/figure><\/div>\n\n\n\n<p>Premi\u00e8re remarque : notre arbre comporte 4\u00b2 = 16 issues. Ensuite, nous avons autant de chance d&#8217;aller vers le Nord, le Sud, l&#8217;Est ou l&#8217;Ouest, \u00e0 savoir 1 chance sur 4. Ainsi, la probabilit\u00e9 d&#8217;obtenir le chemin &#8220;X-Y&#8221;, o\u00f9 \\((X;Y)\\in\\{S;N;O;E\\}^2\\) est \u00e9gale \u00e0 0,25\u00b2, soit 0,0625.<\/p>\n\n\n\n<p>La probabilit\u00e9 de revenir \u00e0 l&#8217;origine est la probabilit\u00e9 d&#8217;avoir les chemins : &#8220;SN&#8221;, &#8220;NS&#8221;, &#8220;EO&#8221; ou &#8220;OE&#8221;, soit \u00e9gale \u00e0 \\(4\\times\\frac{1}{4^2}=\\frac{1}{4}\\).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"une-marche-a-4-pas\"><span class=\"ez-toc-section\" id=\"Une_marche_a_4_pas%E2%80%A6\"><\/span>Une marche \u00e0 4 pas&#8230;<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Tentons maintenant de regarder ce qui se passe si on fait 4 pas (en effet, si on n&#8217;en fait que 3, il est impossible de revenir \u00e0 l&#8217;origine).<\/p>\n\n\n\n<p>Notre arbre compterait \\(4^4 = 256\\) issues (inutile de vous dire que je ne vais pas m&#8217;amuser \u00e0 le tracer!), et chaque chemins menant \u00e0 une issue aurait une probabilit\u00e9 \u00e9gale \u00e0 \\(0,25^4\\).<\/p>\n\n\n\n<p>Les chemins qui font que l&#8217;on se retrouve \u00e0 l&#8217;origine \u00e0 la fin de la marche sont: NNSS, SSNN, NSNS, SNSN puis OOEE, EEOO, OEOE, EOEO, et puis NSEO, SNEO, NSOE, SNOE, OENS, OESN, EONS et EOSN, soit 4\u00b2. <\/p>\n\n\n\n<p>Ainsi, la probabilit\u00e9 de revenir \u00e0 l&#8217;origine est \u00e9gale \u00e0 \\(4^2 \\times \\frac{1}{4^4}=\\frac{1}{4^2}\\). Hum&#8230; Int\u00e9ressant ! On trouve la m\u00eame probabilit\u00e9 que pour une marche \u00e0 2 pas&#8230; Je dis \u00e7a, je dis rien&#8230;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"une-marche-a-10-pas\"><span class=\"ez-toc-section\" id=\"Une_marche_a_10_pas\"><\/span>Une marche \u00e0 10 pas<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>L&#8217;arbre contient \\(4^{10}\\) issues et chaque issue \u00e0 la m\u00eame probabilit\u00e9 d&#8217;\u00eatre obtenue, \u00e0 savoir \\(\\frac{1}{4^{10}}\\). De plus, les chemins qui m\u00e8nent \u00e0 l&#8217;origine du rep\u00e8re doivent comporter autant de &#8220;N&#8221; que de &#8220;S&#8221; et autant de &#8220;O&#8221; que de &#8220;E&#8221;. C&#8217;est ici que \u00e7a se complique car il faut <em>d\u00e9nombrer <\/em>toutes les possibilit\u00e9s.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"avec-aucun-n\"><span class=\"ez-toc-section\" id=\"Avec_aucun_%E2%80%9CN%E2%80%9D\"><\/span>Avec aucun &#8220;N&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Il doit alors y avoir cinq &#8220;O&#8221; et cinq &#8220;E&#8221;. On cherche le nombre d&#8217;anagrammes de &#8220;OOOOOEEEEE&#8221;, qui est:$$\\frac{10!}{5!^2}=252.$$<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"avec-un-n\"><span class=\"ez-toc-section\" id=\"Avec_un_%E2%80%9CN%E2%80%9D\"><\/span>Avec un &#8220;N&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Il y a alors un &#8220;S&#8221;, quatre &#8220;O&#8221; et quatre &#8220;E&#8221;. Notre recherche correspond \u00e0 la recherche du nombre d&#8217;anagrammes de &#8220;SNOOOOEEEE&#8221;, \u00e9gal \u00e0:$$\\frac{10!}{4!^2}=6300.$$<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"avec-deux-n\"><span class=\"ez-toc-section\" id=\"Avec_deux_%E2%80%9CN%E2%80%9D\"><\/span>Avec deux &#8220;N&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>On cherche le nombre d&#8217;anagrammes de &#8220;NNSSOOOEEE&#8221;, qui est:$$\\frac{10!}{2!^2\\times3!^2}=25200.$$<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"avec-k-n-0-leq-k-leq-5\"><span class=\"ez-toc-section\" id=\"Avec_k_%E2%80%9CN%E2%80%9D_0_leq_k_leq_5\"><\/span>Avec <em>k<\/em> &#8220;N&#8221;, \\(0 \\leq k \\leq 5\\)<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>D&#8217;apr\u00e8s ce qui vient d&#8217;\u00eatre vu, on peut ais\u00e9ment conclure que le nombre de chemins comportant <em>k<\/em> &#8220;N&#8221; (et donc <em>k<\/em> &#8220;S&#8221;, 5-<em>k<\/em> &#8220;O&#8221; et 5-<em>k<\/em> &#8220;E&#8221;) est:$$\\frac{10!}{(k!(5-k)!)^2}.$$ On peut aussi raisonner autrement en disant qu&#8217;il faut placer <em>k<\/em> &#8220;N&#8221; parmi les 10 emplacements possibles, puis placer <em>k<\/em> &#8220;S&#8221; parmi les 5-<em>k<\/em> emplacements restants, puis placer 5-<em>k<\/em> &#8220;O&#8221; parmi les 10-2<em>k<\/em> emplacements restants, et enfin placer les 5-<em>k<\/em> &#8220;E&#8221; parmi les 5-<em>k<\/em> emplacements restants, ce qui donne \\(\\displaystyle\\binom{10}{k}\\times\\binom{10-k}{k}\\times\\binom{10-2k}{5-k}\\times\\binom{5-k}{5-k}\\) possibilit\u00e9s, qui est \u00e9gal au r\u00e9sultat pr\u00e9c\u00e9dent (heureusement!).<\/p>\n\n\n\n<p>En ajoutant tous les r\u00e9sultats, on trouve 63504 chemins possibles. Et si vous n&#8217;\u00eates pas convaincus, vous pouvez toujours, avec Python, vous en assurer avec ce programme:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"false\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">L = ['O','E','N','S']\n\nP = []\n\nfor a in L:\n    for b in L:\n        for c in L:\n            for d in L:\n                for e in L:\n                    for f in L:\n                        for g in L:\n                            for h in L:\n                                for i in L:\n                                    for j in L:\n                                        P.append([a,b,c,d,e,f,g,h,i,j])\n\nn = 0\nfor e in P:\n    if e.count('O') == e.count('E') and e.count('N') == e.count('S'):\n        n += 1\n\nprint(n)\n<\/pre>\n\n\n\n<p>Mais personnellement, j&#8217;ai utilis\u00e9 Xcas pour obtenir ce m\u00eame r\u00e9sultat avec le code suivant:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">n:=0;for(k=0;k&lt;=5;k++){n:=n+factorial(10)\/((factorial(k)*factorial(5-k))^2);}<\/pre>\n\n\n\n<p>La probabilit\u00e9 de revenir \u00e0 l&#8217;origine du rep\u00e8re est donc \u00e9gale \u00e0 \\(\\frac{63504}{4^{10}}\\approx0,060562\\). Ben sur le coup, je me suis bien plant\u00e9! Je pensais obtenir une probabilit\u00e9 exactement \u00e9gale \u00e0 \\(\\frac{1}{16}\\) mais ce n&#8217;est a priori pas le cas (m\u00eame si on n&#8217;en est pas loin).<\/p>\n\n\n\n<p>Comme quoi, il faut se m\u00e9fier des intuitions&#8230; surtout quand on sait qu&#8217;elles sont tr\u00e8s souvent mauvaises !<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Marche al\u00e9atoire en Python: c&#8217;est le th\u00e8me d&#8217;un exercices que j&#8217;ai vu dans un livre de sp\u00e9cialit\u00e9 Math niveau 1\u00e8re. Il concerne la marche al\u00e9atoire d&#8217;une puce dans un plan rapport\u00e9 \u00e0 un rep\u00e8re orthonorm\u00e9.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,4,6,5],"tags":[169],"class_list":["post-2031","post","type-post","status-publish","format-standard","hentry","category-enseignement","category-informatique","category-mathematiques","category-python","tag-probabilites"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Marche al\u00e9atoire en Python - Mathweb.fr - D&#039;apr\u00e8s un exercice de maths<\/title>\n<meta name=\"description\" content=\"La marche al\u00e9atoire d&#039;une puce dans un plan rapport\u00e9 \u00e0 un rep\u00e8re orthonorm\u00e9 peut \u00eatre mod\u00e9lis\u00e9e en Python. Voyons cela ensemble.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/\" \/>\n<meta property=\"og:locale\" content=\"fr_FR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Marche al\u00e9atoire en Python - Mathweb.fr - D&#039;apr\u00e8s un exercice de maths\" \/>\n<meta property=\"og:description\" content=\"La marche al\u00e9atoire d&#039;une puce dans un plan rapport\u00e9 \u00e0 un rep\u00e8re orthonorm\u00e9 peut \u00eatre mod\u00e9lis\u00e9e en Python. Voyons cela ensemble.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Mathweb.fr\" \/>\n<meta property=\"article:published_time\" content=\"2020-02-15T15:37:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-01-27T16:35:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2020\/02\/marche-aleatoire.png\" \/>\n<meta name=\"author\" content=\"St\u00e9phane Pasquet\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u00c9crit par\" \/>\n\t<meta name=\"twitter:data1\" content=\"St\u00e9phane Pasquet\" \/>\n\t<meta name=\"twitter:label2\" content=\"Dur\u00e9e de lecture estim\u00e9e\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2020\\\/02\\\/15\\\/marche-aleatoire-et-python\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2020\\\/02\\\/15\\\/marche-aleatoire-et-python\\\/\"},\"author\":{\"name\":\"St\u00e9phane Pasquet\",\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/#\\\/schema\\\/person\\\/e4d3bb07968238378f0d5052a70dcd69\"},\"headline\":\"Marche al\u00e9atoire en Python\",\"datePublished\":\"2020-02-15T15:37:25+00:00\",\"dateModified\":\"2022-01-27T16:35:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2020\\\/02\\\/15\\\/marche-aleatoire-et-python\\\/\"},\"wordCount\":940,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/#\\\/schema\\\/person\\\/e4d3bb07968238378f0d5052a70dcd69\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2020\\\/02\\\/15\\\/marche-aleatoire-et-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/wp-content\\\/uploads\\\/2020\\\/02\\\/marche-aleatoire.png\",\"keywords\":[\"probabilit\u00e9s\"],\"articleSection\":[\"Enseignement\",\"Informatique\",\"Math\u00e9matiques\",\"Python\"],\"inLanguage\":\"fr-FR\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2020\\\/02\\\/15\\\/marche-aleatoire-et-python\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2020\\\/02\\\/15\\\/marche-aleatoire-et-python\\\/\",\"url\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2020\\\/02\\\/15\\\/marche-aleatoire-et-python\\\/\",\"name\":\"Marche al\u00e9atoire en Python - Mathweb.fr - D'apr\u00e8s un exercice de maths\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2020\\\/02\\\/15\\\/marche-aleatoire-et-python\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2020\\\/02\\\/15\\\/marche-aleatoire-et-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/wp-content\\\/uploads\\\/2020\\\/02\\\/marche-aleatoire.png\",\"datePublished\":\"2020-02-15T15:37:25+00:00\",\"dateModified\":\"2022-01-27T16:35:36+00:00\",\"description\":\"La marche al\u00e9atoire d'une puce dans un plan rapport\u00e9 \u00e0 un rep\u00e8re orthonorm\u00e9 peut \u00eatre mod\u00e9lis\u00e9e en Python. Voyons cela ensemble.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2020\\\/02\\\/15\\\/marche-aleatoire-et-python\\\/#breadcrumb\"},\"inLanguage\":\"fr-FR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2020\\\/02\\\/15\\\/marche-aleatoire-et-python\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"fr-FR\",\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2020\\\/02\\\/15\\\/marche-aleatoire-et-python\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/wp-content\\\/uploads\\\/2020\\\/02\\\/marche-aleatoire.png\",\"contentUrl\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/wp-content\\\/uploads\\\/2020\\\/02\\\/marche-aleatoire.png\",\"width\":553,\"height\":471,\"caption\":\"marche al\u00e9atoire\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2020\\\/02\\\/15\\\/marche-aleatoire-et-python\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Marche al\u00e9atoire en Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/#website\",\"url\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/\",\"name\":\"Mathweb.fr\",\"description\":\"Math\u00e9matiques, LaTeX et Python\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/#\\\/schema\\\/person\\\/e4d3bb07968238378f0d5052a70dcd69\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"fr-FR\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/#\\\/schema\\\/person\\\/e4d3bb07968238378f0d5052a70dcd69\",\"name\":\"St\u00e9phane Pasquet\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"fr-FR\",\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/wp-content\\\/uploads\\\/2025\\\/06\\\/cropped-logo-mathweb.webp\",\"url\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/wp-content\\\/uploads\\\/2025\\\/06\\\/cropped-logo-mathweb.webp\",\"contentUrl\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/wp-content\\\/uploads\\\/2025\\\/06\\\/cropped-logo-mathweb.webp\",\"width\":74,\"height\":77,\"caption\":\"St\u00e9phane Pasquet\"},\"logo\":{\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/wp-content\\\/uploads\\\/2025\\\/06\\\/cropped-logo-mathweb.webp\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Marche al\u00e9atoire en Python - Mathweb.fr - D'apr\u00e8s un exercice de maths","description":"La marche al\u00e9atoire d'une puce dans un plan rapport\u00e9 \u00e0 un rep\u00e8re orthonorm\u00e9 peut \u00eatre mod\u00e9lis\u00e9e en Python. Voyons cela ensemble.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/","og_locale":"fr_FR","og_type":"article","og_title":"Marche al\u00e9atoire en Python - Mathweb.fr - D'apr\u00e8s un exercice de maths","og_description":"La marche al\u00e9atoire d'une puce dans un plan rapport\u00e9 \u00e0 un rep\u00e8re orthonorm\u00e9 peut \u00eatre mod\u00e9lis\u00e9e en Python. Voyons cela ensemble.","og_url":"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/","og_site_name":"Mathweb.fr","article_published_time":"2020-02-15T15:37:25+00:00","article_modified_time":"2022-01-27T16:35:36+00:00","og_image":[{"url":"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2020\/02\/marche-aleatoire.png","type":"","width":"","height":""}],"author":"St\u00e9phane Pasquet","twitter_card":"summary_large_image","twitter_misc":{"\u00c9crit par":"St\u00e9phane Pasquet","Dur\u00e9e de lecture estim\u00e9e":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/#article","isPartOf":{"@id":"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/"},"author":{"name":"St\u00e9phane Pasquet","@id":"https:\/\/www.mathweb.fr\/euclide\/#\/schema\/person\/e4d3bb07968238378f0d5052a70dcd69"},"headline":"Marche al\u00e9atoire en Python","datePublished":"2020-02-15T15:37:25+00:00","dateModified":"2022-01-27T16:35:36+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/"},"wordCount":940,"commentCount":4,"publisher":{"@id":"https:\/\/www.mathweb.fr\/euclide\/#\/schema\/person\/e4d3bb07968238378f0d5052a70dcd69"},"image":{"@id":"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2020\/02\/marche-aleatoire.png","keywords":["probabilit\u00e9s"],"articleSection":["Enseignement","Informatique","Math\u00e9matiques","Python"],"inLanguage":"fr-FR","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/","url":"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/","name":"Marche al\u00e9atoire en Python - Mathweb.fr - D'apr\u00e8s un exercice de maths","isPartOf":{"@id":"https:\/\/www.mathweb.fr\/euclide\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/#primaryimage"},"image":{"@id":"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2020\/02\/marche-aleatoire.png","datePublished":"2020-02-15T15:37:25+00:00","dateModified":"2022-01-27T16:35:36+00:00","description":"La marche al\u00e9atoire d'une puce dans un plan rapport\u00e9 \u00e0 un rep\u00e8re orthonorm\u00e9 peut \u00eatre mod\u00e9lis\u00e9e en Python. Voyons cela ensemble.","breadcrumb":{"@id":"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/#breadcrumb"},"inLanguage":"fr-FR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/"]}]},{"@type":"ImageObject","inLanguage":"fr-FR","@id":"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/#primaryimage","url":"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2020\/02\/marche-aleatoire.png","contentUrl":"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2020\/02\/marche-aleatoire.png","width":553,"height":471,"caption":"marche al\u00e9atoire"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mathweb.fr\/euclide\/2020\/02\/15\/marche-aleatoire-et-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.mathweb.fr\/euclide\/"},{"@type":"ListItem","position":2,"name":"Marche al\u00e9atoire en Python"}]},{"@type":"WebSite","@id":"https:\/\/www.mathweb.fr\/euclide\/#website","url":"https:\/\/www.mathweb.fr\/euclide\/","name":"Mathweb.fr","description":"Math\u00e9matiques, LaTeX et Python","publisher":{"@id":"https:\/\/www.mathweb.fr\/euclide\/#\/schema\/person\/e4d3bb07968238378f0d5052a70dcd69"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mathweb.fr\/euclide\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"fr-FR"},{"@type":["Person","Organization"],"@id":"https:\/\/www.mathweb.fr\/euclide\/#\/schema\/person\/e4d3bb07968238378f0d5052a70dcd69","name":"St\u00e9phane Pasquet","image":{"@type":"ImageObject","inLanguage":"fr-FR","@id":"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2025\/06\/cropped-logo-mathweb.webp","url":"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2025\/06\/cropped-logo-mathweb.webp","contentUrl":"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2025\/06\/cropped-logo-mathweb.webp","width":74,"height":77,"caption":"St\u00e9phane Pasquet"},"logo":{"@id":"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2025\/06\/cropped-logo-mathweb.webp"}}]}},"_links":{"self":[{"href":"https:\/\/www.mathweb.fr\/euclide\/wp-json\/wp\/v2\/posts\/2031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mathweb.fr\/euclide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mathweb.fr\/euclide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mathweb.fr\/euclide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mathweb.fr\/euclide\/wp-json\/wp\/v2\/comments?post=2031"}],"version-history":[{"count":0,"href":"https:\/\/www.mathweb.fr\/euclide\/wp-json\/wp\/v2\/posts\/2031\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.mathweb.fr\/euclide\/wp-json\/wp\/v2\/media?parent=2031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mathweb.fr\/euclide\/wp-json\/wp\/v2\/categories?post=2031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mathweb.fr\/euclide\/wp-json\/wp\/v2\/tags?post=2031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}