A Python module is a file containing Python definitions and statements to be used in other Python programs
Python comes with a standard library of modules
The math module provides the user access to the common functions used in many applications in a wide range of disciplines. Additionally, the model provide accurate approximations for irrational numbers usch as \(\pi\) and \(e\). More thorough information can be found by going to the informational web page shown below.
The random
module provides the Python user with a pseudo-random number generator. That is, it is not truly random. The process depends on a seed value the generator uses. Each time the generator is used, the seed value is updated. The process is effectively random because each time you run a program that utilizes the generator, it is very likely that a different seed value is used so that a different sequence of numbers is generated by the algorithm.
The random
module commands we will be using most often are:
random.random()
- that returns a floating point number in the interval [0.0, 1.0).
random.randrange(m, n)
- m and n are integers (m < n), and the function returns one of the following numbers with equal probability : m, m+1, m+2, … , n-2, n-1
Activity 1: Modify the Point plot program, shown below, to graph the square root function \(y = \sqrt{x}\), using the math
module, on the interval \(0 \le x \le 10\). Plot a point for each \(x\) value corresponding to tick locations with a spacing of 0.5.
# Name :
# Point plot a function with ticked x and y axes
import turtle as t
wn = t.Screen()
# graph window
x_min = -10.0
x_max = 10.0
y_min = -10.0
y_max = 10.0
# tick info
t_l = 0.2
x_t_space = 0.5
y_t_space = 0.5
wn.setworldcoordinates(x_min, y_min, x_max, y_max)
alex = t.Turtle()
# Draw x axis
alex.up()
alex.goto(x_min, 0.0)
alex.down()
alex.goto(x_max, 0.0)
alex.up()
# Draw the y axis
alex.goto(0.0, y_min)
alex.down()
alex.goto(0.0, y_max)
alex.up()
# Draw the x tick marks
n_x_ticks = int((x_max-x_min)/x_t_space) + 1
for tick in range(n_x_ticks):
loc = x_min + tick*x_t_space
alex.up()
alex.goto(loc, -t_l*0.5)
alex.down()
alex.goto(loc, t_l*0.5)
alex.up()
# Draw the y tick marks
n_y_ticks = int((y_max - y_min)/y_t_space) + 1
for tick in range(n_y_ticks):
loc = y_min + tick*y_t_space
alex.up()
alex.goto(-t_l*0.5, loc)
alex.down()
alex.goto(t_l*0.5, loc)
alex.up()
# Plot the function points
alex.color('blue')
alex.up()
for tick in range(n_x_ticks):
x = x_min + tick*x_t_space
alex.goto(x, x*x/10 - 5)
alex.dot(2)
# always the last line
wn.exitonclick()
Activity 2: Use the random
and turtle
modules to write a program that generates and plots the random motion of a turtle.
Use the method wn.setworldcoordinates(x_min, y_min, x_max, y_max)
to set the coordinates using x_min = y_min = -30, and x_max = y_max = 30.
Draw a set of axis in black. Use a tick spacing of 5 units for both axes.
Using a for
loop, have the turtle’s direction determined in a random fashion among four directions : east, south, west, and north. For each step, have the turtle move forward 2 units. Use the range
function with a range of 20 loops to begin. Have the turtle plot print a dot()
at the end of each step. The size of the dot’s diameter can be set with an integer input. A value of 6 or 8 maybe good.
Activity 3:
Modify the program in the previous activity to include the randomization of the turtle’s step length. A random length must be taken for the continuous interval of [0.5, 3.0)
Modify the program so that the color of the turtle’s path is randomly selected for each step.
Modify the program so that the number of possible directions may be set using a single parameter called “n_dirs”
Coding Exercise 1
import random
total = 0
for i in range(10):
number = 1 + random.random()*4
print(number)
total += number
print("Average =", total / 10)