HomeTurtlegraficsGPanelRobotics WebTigerPython |
Python - Online |
Deutsch English |
YOU LEARN HERE... |
that you can execute certain program parts several times with the help of the repeat loop repeat. Random numbers form the basis of many simulations. They are generated using a random algorithm, which is contained in thel random module in Python. |
EXAMPLES |
Program:
The randint(a, b) command returns a random integer number in the range a to b (including both limits). For example, random dice numbers are obtained with randint(1, 6). The random() function generates random numbers between 0 (included) and 1 (excluded) with 10 decimal places (e.g. 0.69880747453)(ausgeschlossen) mit 10 Dezimalstellen erzeugt (z.B. 0.69880747453) In the example, 100 small red circles are to be randomly distributed over the area of the graphics window. First, the position of the circle is defined with two random numbers in the range 0 to 100 and a circle is drawn. With repeat 100: this process is repeated 100 times. Program:
|
REMEMBER YOU... |
You can use repeat() to execute program blocks several times. Python also has while and for loops, but these require the concept of variables and will therefore be introduced later. from random import randint imports the randint(a, b) function from the random module. This returns an integer random number in the range a to b (including both limits). |
TO SOLVE BY YOURSELF |
1) |
|
2) |
Draw connecting lines from point (0, 0) to 200 randomly selected points. |
3) |
In the adjacent “work of art”, the position and radius of the filled circles and also the colors are randomly selected and result in a completely different image each time the program is executed. To create the colors, use the command Zufällige Farben erhält man mit: setColor(randint(0, 255), randint(0, 255), randint(0, 255)) |