CPSC150A
Scientific Computing

Activity 10

Modules

Dice

With the random module you can create programs that appear to be unpredicable, like the rolling of dice.

Details

Create a python program that simulates rolling a pair of six-sided dice. The program should prompt the user to enter the number of rolls, print all of the rolls, and the average of the rolls.

  • Inside of a loop, use the random module's randrange function to repeately generate two integers in between 1 and 6.
  • Use an accumulator to compute the sum of all of the dice rolls.
  • After the loop, use the accumulated sum to compute the average.

Example

Enter the number of rolls:
                                                              3
2 2
1 2
4 1
The average roll is 4.0

Challenge

What's the difference between using two six-sided dice and three four-sided dice? They both have a maximum value of 12, but do they have different average values? Write program that simulates rolling both kinds of dice and prints the average of both. Be sure to "roll" the dice many times to get an accurate average.

Sine

Hopefully you are familiar with the sine and cosine functions from back in your trigonometry class. As you can imagine, these are incredibly useful functions not only for mathematics, but for computer drawing and animation.

Details

Create a Python program that uses the turtle module to draw a plot of the math.sin function. The program should prompt the user for the amplitude and period of the sin function, in turtle window units.

Example

Enter the amplitude:
                                                            100
Enter the period:
                                                            600
  • You are going to approximate the sine wave curve by sampling the sine function. Sampling is the process of computing something for some points in a space, as opposed to computing something for the whole space. You are going to choose a set of equally spaced points on the curve. Use a for loop that starts at the left part of the window, and increases the value by a fixed step until it gets to the right portion of the window.
  • Treat the input to the math.sin function as the x coordinate of the point on the turtle window, and the return value as the y coordinate. The turtle should go to each one of these points in a sequence using the turtle.setposition function.
  • The math.sin function always returns a value between -1 and 1. You need to use the amplitude to increase the size of this output, so you can visually see the actual sine wave. You can do this by multiplying the output by amplitude.
  • The math.sin function's input is an angle in radians. If the input x coordinte is not scaled, the period of the plot will be 2 π. To change the period, multiply the input x coordiate by 2 π over the desired period.

Challenge

The sine and cosine functions are interesting, but I think the tangent function is a much more visually appealing function. Change the color of the turtle's pen, and also plot the tangent function on top of the sine function.