HomeTurtlegraficsGPanelRobotics WebTigerPython
 Python - Online
zufallszahlen
Deutsch   English   

8. RANDOM NUMBERS

 

 

YOU LEARN HERE...

 

how you can generate random numbers. Since chance plays an extremely important role in everyday life, you can use random numbers to simulate such processes on the computer. As with a throw of the dice, random numbers are usually equally distributed, i.e. they occur with the same frequency (probability).

 

 

EXAMPLES

  You draw 40 stars and define their position in the turtle window with random numbers. To do this, you need the randint() function from the random module, which you must first import.

The randint(a, b) function returns a random integer number in the range a, b each time it is called. For example, you get random dice numbers with randint(1, 6).

In your program, you draw 40 stars at random positions. The turtle window has a coordinate system with the zero point in the center and x and y in the range approx. -300 to 300. For the position of the stars, select random numbers in the range -250... 250 for the x and y coordinates. 250. The star() function draws a single star.

 


Program:    

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)    
► Copy to clipboard
 

 

You can also choose the colors randomly. The red, green and blue color components of a color can be generated as random numbers in the range 0 to 255.
r = randint(0, 255)
g = randInt(0, 255)
b = randInt(0, 255)
setFillColor(r, g, b)creates a random fill color. Draw the blue background with the clear("darkblue")command.

 

 

Program:    

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)    
► Copy to clipboard
 

 

Random Walk
The Turtle starts at the home position (0, 0) and moves forward repeatedly (e.g. 50 times) by 30 steps in a randomly selected direction. To set the direction of movement, use the function setHeading(angle). angle is a random number between 0 and 360.

 

Program:    

from gturtle import *
from random import randint

makeTurtle()

repeat 50:
    angle = randint(0, 360)
    setHeading(angle)
    forward(30)
► Copy to clipboard

 

 

REMEMBER...

 

from random import randint mports the randint() function from the random module. randint(a, b) generates a random integer number between a and b, where a is the smallest and b is the largest number.

 

 

TO SOLVE BY YOURSELF

  1.
Draw 100 red, 100 blue and 100 yellow confetti (dots) at random positions.  


  2.

Random-corner
In your program, a random integer n between 3 and 12 is generated for each run. The Turtle then draws a corresponding regular n-corner and fills it with a color of your choice. Adjust the side length accordingly.

 

 
 

 

3.

Everything is random: position, side length and color of the squares. Create similar artworks. Think about how pictures 2 and 3 were created.