CS 152: Lab 4

Title image Project 4
Fall 2019

Lab Exercise 4: The Random Package and matplotlib

The goal of this week's project is to get further experience with module, hierarchical design. In the lab, we'll go over the Python random package and some basic capabilities of the plotting package matplotlib.


Tasks

If you have not already done so, mount your personal space, create a new project4 folder and bring up TextWrangler and a Terminal.

  1. Creating Random Numbers

    In Python, we create random numbers using the random package. Start a new file, rand.py, and put your standard header at the top. Then write

    import random
    1. Write a function that generates N random numbers between 0 and 1

      Define a function genNrandom that has one argument N which is the number of random numbers to create.

      1. The first step is to assign to a variable (e.g. numbers) an empty list.
      2. The second step is to start a for loop that executes N times. Each time through the loop, append to your numbers list the result of calling the function random.random().

        The function random.random() returns a randomly generated floating point number between 0 and 1. It is one of the useful functions in the random package.

      3. Return the numbers list
    2. Test the function

      Define a test function. The test function should assign to a variable the result of calling genNrandom with an argument of 10. It should then use a loop to print out the 10 numbers. Run your code and make sure it works properly.

    3. Write a function that generates N random inegers between a lower bound and an upper bound

      Define a function genNintegers that has three arguments: the number of points to create, a lower bound, and an upper bound. All three arguments should be integers.

      1. In the function assign to a variable (e.g. numbers) an empty list.
      2. Loop N times and each time through the loop append the result of calling random.randint( lowerBound, upperBound ) to your numbers list.

        The function random.randint( L, B ) returns a randomly generated number between L and B, inclusive.

      3. Return the number list
      4. Add code to your test function to call and then print out the result of calling genNintegers with arguments of 10, -10, and 10. Make sure it produces expected values.
    4. Write a function that generates N random values drawn from a normal/Gaussian distribution

      Define a function genNnormal that has three arguments: the number of points to create, a mean, and a standard deviation. The second and third arguments can be integers or floating point values.

      1. In the function assign to a variable (e.g. numbers an empty list.
      2. Loop N times and each time through the loop append the result of calling random.gauss( mean, std ) to your numbers list.

        The function random.gauss( mean, std ) returns a randomly generated number drawn from a normal or Gaussian distribution with the given mean and standard deviation.

      3. Return the number list
      4. Add code to your test function to call and then print out the result of calling genNnormal with arguments of 10, 0, and 0.2. Make sure it produces expected values.

  2. Creating plots with matplotlib

    Continuing with the same file, add a new import statement at the top. If you are using your own laptop, you may want to switch to one of the lab computers, as you may need to install matplotlib in order to use it on your own machine.

    import matplotlib.pyplot as plt

    Now plot some of the numbers you created in the prior exercise.

    In the test function, you should have three variables that all contain random numbers. One holds values between 0 and 1 (call it x), one holds values between -10 and 10 (call it y), and one holds values distributed around zero (call it z). The following walks through the process of plotting x versus z.

    1. At the end of the test function, call the function plt.plot() with the arguments x, z, and the string 'o'. The lists x and z must have the same number of elements.
    2. After the call to plot, call the function plt.show(). Then run your code. It should create a simple plot.
    3. After the call to plot and before the call to show, you can modify aspects of the plot. For example, plt.title() sets the title to whatever string you pass in as an argument.
    4. Set the X axis label using plt.xlabel() with "X" as the argument.
    5. Set the Y axis label using plt.ylabel() with "Y" as the argument.

    Now you know how to generate simple plots with an x-label, a y-label, and a title. Look at the matplotlib documentation for PyPlot for more information on how to create more complex graphs and charts.


  3. The following instructions in a terminal will let you install matplotlib for python3 on macOS. The sudo command requires you to type your password before continuing.

    curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
    sudo python get-pip.py
    sudo pip3 install matplotlib

    For guidance on installing on other platforms, see the matplotlib installation page.


When you are done with the lab exercises, please begin the project.