HomeTortue graphiqueGPanelRobotique WebTigerPython |
Python - Online |
TU APPRENDS ICI... |
comment tu peux générer des nombres aléatoires. Comme le hasard joue un rôle extrêmement important dans la vie quotidienne, tu peux utiliser des nombres aléatoires pour reproduire (simuler) de tels processus sur l'ordinateur. Comme pour un lancer de dé, les nombres aléatoires sont généralement distribués de manière égale, c'est-à-dire qu'ils apparaissent avec la même fréquence (probabilité). |
EXEMPLES |
Tu dessines 40 étoiles et tu définis leur position dans la fenêtre Turtle avec des nombres aléatoires. Pour cela, tu as besoin de la fonction randint() du module random, que tu dois d'abord importer.
Programme: from gturtle import * from random import randint def star(): startPath() repeat 5: forward(50) left(144) fillPath() makeTurtle() setFillColor("blue") hideTurtle() repeat 40: star() x = randint(-250, 250) y = randint(-250, 250) setPos(x, y) |
Programme: from gturtle import * from random import randint def star(): startPath() repeat 5: forward(50) left(144) fillPath() makeTurtle() hideTurtle() clear("darkblue") repeat(50): star() r = randint(0, 255) g = randint(0, 255) b = randint(0, 255) setFillColor(r, g, b) x = randint (-300, 300) y = randint (-250, 250) setPos(x, y) |
Programme: from gturtle import * from random import randint makeTurtle() repeat 50: angle = randint(0, 360) setHeading(angle) forward(30) C'est encore plus facile si vous utilisez la fonction setRandomHeading(). La direction de la tortue est choisie au hasard. Programme:   from gturtle import * makeTurtle() repeat 50: setRandomHeading() forward(30) |
REMARQUE... |
from random import randint importe la fonction randint() du module random. randint(a, b) génère un nombre entier aléatoire entre a et b, où a est le plus petit et b le plus grand nombre. |
À FAIRE PAR TOI-MÊME |
1. |
|
|||||
2. |
|
|
![]() |
![]() |
![]() |