Lesson17 Individual Assignment

Individual means that you do it yourself. You won't learn to code if you don't struggle for yourself and write your own code. Remember that while you can discuss the general (algorithmic) way to solve a problem, you should not even be looking at anyone else's code or showing anyone else your code for an individual assignment.
Review the Group Work guidelines on Cavas and/or ask an instructor if you have any questions.

Programming Practice

Be sure to spell all function names correctly - misspelled functions will lose points (and often break anyway since no one is sure what to type to call it). If you prefer showing your earlier, scratch work as you figure out what you are doing, please be sure that you make a final, complete, correct last function in its own cell that you then call several times to test. In other words, separate your thought process/working versions from the final one (a comment that tells us which is the final version would be lovely).

Every function should have at least a docstring at the start that states what it does (see Lesson3 Team Notebook if you need a reminder). Make other comments as necessary.

Make sure that you are running test cases (plural) for everything and commenting on the results in markdown. Your comments should discuss how you know that the test case results are correct.

part 1: Simulating Dice

A. Before you code, provide:

  • an equation that will take the output from random.random and return a floating-point number between 1 and 7 (based on your answers to the Lesson 17 pre-activity)
  • a way in Python how you can convert a floating-point number to an integer. Will your method round or truncate the floating-point number?
  • an explanation how you would test your dice function to make sure it works correctly

B. Define a dice function that:

  • takes no parameters and
  • returns a number between 1 and 6
  • your dice function should:
    • use the python random.random function
    • start by converting the range from [0, 1) to [1, 7)
    • convert the floating-point number between [1, 7) to an integer between 1 and 6
    • not print anything in its final version, if you use prints to help you debug, you should comment them out once your function works correctly.

Start by having your dice function return a floating-point number between [1, 7) and be sure it works properly before trying to get your code to return an integer.

C. Define a roll_dice_repeat function that:

  • takes one parameter n, which is the number of random numbers to generate
  • returns a list of six values, where each value is the number of times each integer is generated
  • your roll_dice_repeat function should:
    • call your dice function repeatedly
    • keep track of the number of times each integer (1 through 6) is generated
    • The first position in the returned list should represent how many times a 1 is rolled, and the sixth position in the list should represent how many times a 6 is rolled
    • not print anything in its final version, if you use prints to help you debug, you should comment them out once your function works correctly.

After you have this function working, run this function and then comment on the results. How do the results convince you that your dice function works?

part 2: Graphing Random Numbers

D. Define a binning function that:

  • takes two parameters:
    • n (the number of random numbers to generate) and
    • m (the number of intervals to divide the range of floating point numbers)
  • returns a list with the number of entries in each interval (or bin).
  • your binning function should:
    • call the Python random.random function n times
    • divide the floating point numbers [0, 1) into a range of m intervals
    • keep track of the number of random numbers that fall into each range
    • not print anything in its final version, if you use prints to help you debug, you should comment them out once your function works correctly.

Note: This new binning function should be a more general version of your functions from part 1, so the pseudocode for this binning function is almost identical to what you have implemented in part 1 (although in one function instead of two). Whereas the dice function always divided the range of floating point numbers [1, 7) into a range of six intervals [1, 6], the binning function should call random.random n times and convert the numbers from a floating-point number between [0, 1) to an integer between [1, m].

After running the binning function repeatedly, describe the type of the distribution observed for the random numbers generated, i.e., is there a pattern/trend for the number of random values in each interval?

E. Write a graphical_binning function that:

  • takes two parameters (n and m)
  • returns nothing
  • your graphical_binning function should:
    • call the binning function
    • use Python code to graphically plot your binning results
    • not print anything in its final version, if you use prints to help you debug, you should comment them out once your function works correctly.

Refer back to the Lesson 17 team to review how to make graphs/plots.

F. Modify the graphical_binning function. The graphical_binning function from the last step will make it easier to quickly create plots using different input parameters for comparison. However before making any comparisons, you should normalize the range of output values so that the sum of all intervals is the same regardless of the value of n.

The easiest way to do this is to divide the number of elements in each bin by the total number of values generated. This ensures that the sum of the elements for all bins is 1, and the value for each bin is limited to the range of 0 to 1. Modify your binning function so that the output list is normalized.

G. Run and test your graphical_binning function to demonstrate that it meets all of the criteria above. Make sure to comment on your results.


In [ ]: