In this Python Turtle tutorial, we will learn How to create a random function in python turtle and we will also cover different examples related to Turtle Random. And, we will cover these topics.
- Python turtle random
- Python turtle random dots
- Python turtle random colors
- Python turtle random walk
- Python turtle random movement
- Python turtle random pen color
- Python turtle random direction
- Python turtle random shapes
- Python turtle random number
- Python turtle random randint
Python turtle random
In this section, we will learn about how to create a random function in python turtle.
Before moving forward we should have a piece of knowledge about random function.
Random is defined as an action that is happening without any method or we can say that it is unfamiliar or unspecified to us. Turtle random function is that which randomly chooses the position, direction where the turtle is placed and starts to make shapes.
Code:
In the following code, we will import the turtle library from turtle import *, import turtle, and also import random function from random import randint for generating the random objects.
- The turtle() method is used to make objects.
- tur.shape(‘turtle’) is used to give the turtle shape to pen.
- tur.penup() is used to stop drawing.
- tur.goto(randint(-150,0),randint(0,150)) is used to generating random things on the screen.
from turtle import *
import turtle
from random import randint
tur = turtle.Turtle()
tur.shape('turtle')
tur.penup()
tur.goto(randint(-150,0),randint(0,150))
turtle.done()
Output:
After running the above code we get the following output in which we can see that the turtle is placed on the randomly generated position.
Read: Python turtle draw letters
Python turtle random dots
In this section, we will learn about how to create random dots in python turtle.
As we know random means the thing which is unfamiliar to us or generated randomly. Here also we can use the randint() function which generates random numbers and with the help of these random numbers, random dots are generated. These random dots are drawn with the help of a turtle.
Code:
In the following code, we will import the turtle library from turtle import *, import turtle. The turtle() method is used to make objects.
- tur.dot() is used to generate the dots.
- tur.forward(spce) is used to move the turtle in the forward direction.
- tur.backward(spce*a) is used to move the turtle in the backward direction.
- tur.right(90) is used to move the turtle in the right direction.
- tur.left(90) is used to move the turtle in the left direction.
- tur.penup() is used to top the drawing.
- tur.hideturtle() is used for hiding the turtle from the screen.
from turtle import *
import turtle
tur = turtle.Turtle()
def draw(spce,a):
for i in range(a):
for j in range(a):
tur.dot()
tur.forward(spce)
tur.backward(spce*a)
tur.right(90)
tur.forward(spce)
tur.left(90)
tur.penup()
draw(10,8)
tur.hideturtle()
turtle.done()
Output:
After running the above code, we get the following output in which we can see that the random dots are generated on the screen.
Read: Python turtle input with examples
Python turtle random colors
In this section, we will learn about how to create random colors in python turtle.
As we know the random function is used to generate random numbers and these random numbers help the turtle for generated the random colors. The turtle picks the color randomly and this randomly chosen color is shown on the screen.
Code:
In the following code, we will import the turtle library from turtle import *, import turtle as tur, and also import random module from random import randint for generating random numbers. The turtle() method is used to make objects.
- tur.speed(0) is used to give the speed to the turtle.
- tur.pensize(10) is used to give a size to the turtle.
- tur.colormode(255) is used to show every color type.
- tur.color(randint(0, 255) is used to generating the random colors.
- begin_fill() is used to fill the color inside the shape.
- tur.circle(20) is used to generate the circle with the help of a turtle.
- end_fill() is used to end filling color.
- tur.goto(randint(-500, 500), randint(-300, 270)) is used to show the accurate position of generating color.
from turtle import *
import turtle as tur
from random import randint
tur.speed(0)
tur.pensize(10)
tur.colormode(255)
while True:
tur.color(randint(0, 255),
randint(0, 255),
randint(0, 255))
begin_fill()
tur.circle(20)
end_fill()
tur.penup()
tur.goto(randint(-500, 500), randint(-300, 270))
tur.pendown()
Output:
After running the above code, we get the following output in which we can see that the random color is generated on the screen.
Read: Python Turtle Grid
Python turtle random walk
In this section, we will learn about how the turtle walks randomly on the screen in the python turtle.
As we know the turtle is used to draw different shapes and pictures on the screen. And we also know the random function is used to generate a random number that helps the turtle to walk randomly on the screen.
Code:
In the following code, we will import the turtle library from turtle import *, import turtle as tur, and also random module import random. The turtle() method to make objects.
- tur.title(“Python Guides”) is used to give the title to the screen.
- a1.color(‘cyan’) is used to give the color to the turtle.
- tur.speed(0) is used to give the speed to the turtle.
from turtle import *
import turtle as tur
import random
import math
tur.setup(900,500)
tur.title("Python Guides")
a1 = tur.Turtle()
b1 = tur.Turtle()
c1 = tur.Turtle()
d1 = tur.Turtle()
e1 = tur.Turtle()
a1.color('cyan')
b1.color('red')
c1.color('purple')
d1.color('yellow')
e1.color('red')
turtlist = []
turtlist.append(a1)
turtlist.append(b1)
turtlist.append(c1)
turtlist.append(d1)
turtlist.append(e1)
tur.speed(0)
tur.tracer(0)
tur.hideturtle()
sum = 0
count = 0
for j in range(100):
for i in range(10000):
for turt in turtlist:
turt.seth(random.randrange(0,360,90))
turt.fd(10)
tur.update()
for turt in turtlist:
sum += math.sqrt(turt.xcor()*turt.xcor() + turt.ycor()*turt.ycor())/10*2*math.sqrt(turt.xcor()*turt.xcor() + turt.ycor()*turt.ycor())/10*2/100
count += 1
for turt in turtlist:
turt.clear()
turt.up()
turt.goto(0,0)
turt.down()
print(sum/count)
Output:
After running the above code, we get the following output in which we can see multiple turtles are used and each turtle walks in a different direction and draw the pattern.
In the below output we can see how much distance is covered by the turtle after walking.
Read: Python Turtle Pen + Examples
Python turtle random movement
In this section, we will learn about how turtle moves randomly in python turtle.
As we know turtle is used to draw shapes and pictures on the screen. Here we can see the random movement of the turtle. Turtle move randomly on the screen with the help of a random function. The random function generated the random numbers with the help of these random numbers turtle move randomly.
Code:
In the following code, we will import turtle library from turtle import *, import turtle as tur. The turtle() method is used to make objects.
- tur.right(random.randint(0,360)) is used to generate random numbers which helps the turtle to move.
- tur.right(180) is used to move the turtle in the right direction.
- tur.forward(300) is used to move the turtle in the forward direction.
from turtle import *
import turtle as tur
import random
count = 0
while count <51:
count += 1
if (tur.xcor() >-300 and tur.xcor() <300) and\
(tur.ycor() >-300 and tur.ycor() <300):
tur.forward(random.randint(30,100))
tur.right(random.randint(0,360))
else:
tur.right(180)
tur.forward(300)
Output:
After running the above code we get the following output in which we can the turtle move randomly on the screen.
Read: Python Turtle Star – How to Draw
Python turtle random pen color
In this section, we will learn about how random pen color is generated in python turtle.
As we know the randint() function is used to generate the random numbers and these random numbers help the pen to pick the pen color randomly and draw the shapes and pictures on the screen.
Code:
In the following code, we will import the turtle library from turtle import *, import turtle. The turtle() method is used to make objects.
- tur.color(“yellow”) is used to give the color to the pen.
- tur.forward(100) is used to move the turtle in the forward direction.
- tur.right(60) is used to move the turtle in the right direction.
- tur.color(random.choice(colors)) is used for selected the color randomly for the pen.
from turtle import *
import turtle
import random
tur=turtle.Turtle()
colors=["yellow","pink","red","purple","light blue"]
tur.color("yellow")
for i in range(10):
for i in range(2):
tur.forward(100)
tur.right(60)
tur.forward(100)
tur.right(120)
tur.right(36)
tur.color(random.choice(colors))
turtle.done()
Output:
After running the above code, we get the following output in which we can see that the random pen color is generated with the help of these pen colors we can draw shapes on the screen.
Read: Python Turtle Cheat Sheet
Python turtle random direction
In this section, we will learn about how to get turtle random direction in python turtle.
As we know the random function is used for generating random numbers and these random numbers help the turtle for direction. The turtle moves randomly in any direction and draws the shape.
Code:
In the following code, we import the turtle library from turtle import *, import turtle, and also import random module for generating random numbers. The turtle() method is used to make objects.
- tur.shape(‘turtle’) is used to give the turtle shape to the pen.
- tur.left(90) is used to move the turtle in the left direction.
- tur.right(90) is used to move the turtle in the right direction.
- tur.forward(50) is used to move the turtle in the forward direction.
from turtle import *
import random
import turtle
def screen(ws, tur):
if random.random() > 0.1:
return True
else:
return False
tur = turtle.Turtle()
ws = turtle.Screen()
tur.shape('turtle')
while screen(ws, tur):
coin = random.randrange(0, 2)
if coin == 0:
tur.left(90)
else:
tur.right(90)
tur.forward(50)
ws.exitonclick()
Output:
After running the above code, we get the following output in which we can see that the turtle moves randomly in any direction.
Read: Python Turtle Oval
Python turtle random shapes
In this section, we will learn about how to draw random shapes in python turtle.
Before moving forward we should have a piece of knowledge about shapes. The shape is the form of objects. There are different shapes like squares, rectangles, and ovals. These shapes are generated randomly with the help of random functions.
Code:
In the following code, we will import the turtle library from turtle import *, import turtle, and also import random module to generate random numbers. The turtle() method is used to make objects.
- random.randrange(-150, 150) is used to generate the random shape with their starting and ending position.
- drawSquare(leng) is used to draw the shape of square.
- drawTriangle(leng) is used to draw the shape of triangle.
- drawCircle(leng) is used to draw the shape of circle.
- turtle.forward(leng) is used to move the turtle in the forward diection.
- turtle.right(ang) is used to move theturtle in the right direction.
- turtle.penup() is used to stop drawing.
- turtle.goto(a, b) is used to move the turtle to its absolute position.
- turtle.pendown() is used to start drawing.
from turtle import *
import turtle
import random
def drawRandom():
a = random.randrange(-150, 150)
b = random.randrange(-150, 150)
leng = random.randrange(75)
shape = random.randrange(1,4)
moveturtle(a,b)
if shape == 1:
drawSquare(leng)
elif shape == 2:
drawTriangle(leng)
elif shape == 3:
leng = leng % 4
drawCircle(leng)
import turtle
def drawshape(side, leng):
ang = 360.0 / side
for side in range(side):
turtle.forward(leng)
turtle.right(ang)
def moveturtle(a, b):
turtle.penup()
turtle.goto(a, b)
turtle.pendown()
def drawSquare(leng):
drawshape(4, leng)
def drawTriangle(leng):
drawshape(3, leng)
def drawCircle(leng):
drawshape(360, leng)
for shape in range(100):
drawRandom()
turtle.done()
Output:
After running the above code, we get the following output in which we can see that random shapes are generated on the screen.
Read: Python Turtle Background
Python turtle random number
In this section, we will learn about how random numbers are generated in python turtle.
Random numbers are generated randomly with the help of a random function. Random function pick the number randomly and show it on the screen.
Code:
In the following code, we will learn to import turtle library from turtle import *, import turtle, and also imports the random module for generating the random numbers.
- random.randint(0, 15) is used to generate random number between a given positive range.
- print(“Random number between 0 and 15 is % s” % (a1)) is used for printing the random number after genrating.
from turtle import *
import turtle
import random
a1 = random.randint(0, 15)
print("Random number between 0 and 15 is % s" % (a1))
a2 = random.randint(-15, -1)
print("Random number between -15 and -1 is % d" % (a2))
a3 = random.randint(-10, 10)
print("Random number between -10 and 10 is % d" % (a3))
Output:
After running the above code we get the following output in which we can see the random number is generated with the help of a random function.
Read: Python Turtle Hide
Python turtle random randint
In this section, we will learn about how to use the turtle random randint function in python turtle.
The randint() function returns the integer value from the specific range. This is the inbuilt function of the random module. This random function is used for generating random numbers.
Code:
In the following code, we will import the turtle library from turtle import *, import turtle, and also import a random module for generating the random number. The turtle() method is used to make objects.
- ws.bgcolor(“cyan”) is used the background color to the screen.
- tur.begin_fill() is used to fill the color in the shape.
- tur.circle(a) is used to draw the shape of a circle.
- tur.end_fill() is used to end filling color.
- tur.up() is used to pick up the pen.
- tur.forward(c) is used for moving the turtle in the forward direction.
- tur.down() is used to start drawing.
from turtle import *
import random
import time
import turtle
tur = turtle.Pen()
ws = turtle.Screen()
ws.bgcolor("cyan")
ws.title("Python Guides")
def drawcircle(yellow, blue, red):
tur.color(yellow, blue, red)
tur.begin_fill()
a = random.randint(10,100)
tur.circle(a)
tur.end_fill()
tur.up()
b = random.randint(0,360)
tur.seth(b)
# t.acor() is turtle's x; t.ycor() is turtle's y
if tur.xcor() < -300 or tur.xcor() > 300:
tur.goto(0, 0)
elif tur.ycor() < -300 or tur.ycor() > 300:
tur.goto(0, 0)
c = random.randint(0,100)
tur.forward(c)
tur.down()
for x in range(0, 100):
i = random.randint(0,100)/100.0
j = random.randint(0,100)/100.0
k = random.randint(0,100)/100.0
drawcircle(i, j, k)
time.sleep(10)
Output:
After running the above code, we get the following output in which we can see the random circles are generated on the screen.
You may also like to read the following tutorials on Python Turtle.
- Python Turtle Draw Line
- Python Turtle Nested Loop
- Python Turtle Get Position
- Python Clear Turtle
- Python Turtle Clock
So, in this tutorial, we discussed Python Turtle Random and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.
- Python turtle random
- Python turtle random dots
- Python turtle random colors
- Python turtle random walk
- Python turtle random movement
- Python turtle random pen color
- Python turtle random direction
- Python turtle random shapes
- Python turtle random number
- Python turtle random randint
Entrepreneur, Founder, Author, Blogger, Trainer, and more. Check out my profile.