Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit



MATH 1MP3 Final Project

 

 

Important notes, read carefully!

Any file submitted for grading that does not conform to the following

specifications will lead to a grade of zero on the project.

1. To start the final project, download the Jupyter notebook file

 

Final_Project_Template.ipynb

which is on Avenue, in Assignments folder.

2. Your project must be submitted as a notebook Jupyter python file and a PDF file. Rename the file that you downloaded to

yourmacid_Final_Project.ipynb

where yourmacid is replaced with your macid from your McMaster email ad- dress.  So if your McMaster email address is [email protected], then you should name your file pangw6_Final_Project.ipynb

3. Write you codes in the denoted place (# insert your code here), do not alter any other part of this template. The code that you enter cannot contain any import statements. All necessary modules have been imported in the first code cell.

4. To complete your assignment, enter your code for each question as suggested by the template.

5. The code that you use only from lecture note week one to week twelve.  Note that the template contains several print statements that, when executed, will test your code.

6. When finished, run all cells, and download it as a Jupyter notebook and a pdf file with all results and then upload both files into Final_Project submission folder on Avenue, by the deadline. If you submit your assignment several times, make sure you submit both Jupyter notebook and pdf file every time. Then only the latest submission will be graded. Be mindful of the penalties for late work.



Project Description

This project is worth 50 points; the point of each question is in the square brackets next to the problem number

In this project you will simulate the playing of a simple game of dice. Here is a descrip- tion of this game:

• The game is played with two 6-sided dice and consists of a number of rolls of the dice. Each roll of the dice will be referred to as a round of the game.

• The outcome of rolling a die (the singular form of the plural word dice) is an integer from 1 through to 6. We assume that the dice are fair and so any one of the 6 possible outcomes is equally likely to occur.

•  If the sum of the two dice rolled is the number N, then it is said that the number N was rolled. For example, if the two dice rolled produce the numbers 3 and 4, then we say that a 7 was rolled (since 3+4 = 7). Note that when rolling two 6-sided dice, the only possible sums are the integers from 2 to 12, inclusive.

•  In round one, after the dice have been rolled

1.  if the sum of the two dice rolled is either 7 or 11, the player immediately wins the game

2.  if the sum of the two dice rolled is 2, 3, or 12, the player immediately loses the game

3.  if the sum of the two dice rolled is any other number (so one of 4, 5, 6, 8, 9, or

10) then that sum is called the point for this game, and the game continues on to the next round

•  If the game continues past round one, then the player continues to roll the two dice until one of the two following events occur:

1.  a 7 is rolled, i.e., the sum of the two dice rolled in the current round is equal to 7. In this case, the player loses the game

2.  the point is rolled again, i.e., the sum of the two dice rolled in the current round sum to the point that was established in round one of the game. In this case, the player wins the game

if some number other than 7 or the point is rolled, then the game continues on to the next round and the dice are rolled again

Therefore, playing this game could take arbitrarily many rounds.  In practice, games will end after a very few rolls, but there is no certainty to this. Here are a few sample plays of this game. Each of the following lists of numbers represents the dice rolls in the corresponding rounds of the game.

•  {10, 9, 9, 9, 6, 7}: The player loses this game, since in round one, the point of 10 is established. The player continued to roll the dice and ended up rolling a 7 in round six, before rolling the point of 10.

•  {12}: The player loses this game in round one since a 12 was rolled.


•  {9, 9}: The player wins this game.

•  {11}: The player wins this game.

In this project, you will produce Python code to simulates playing this game and to visualize the outcome of playing this game multiple times. To do so, you will need to provide code for the following functions.

 

a) [6] roll_dice(num_round):

Given an integer num_round, write a function roll_dice()that return a 2-dimensional Numpy array with shape as (2, num_round) which represent the result of rolling of two  dices num_round times.

For example, your function could return a 2-dimensional Numpy array of [[5 2 6 4 6], [6 1 5 6 6]], for given num_round = 5.

Hint: You must use thenumpy random submodule to generate the integers between 1 and 6, inclusively. Because we assume the dice are fair, you must use a proper function to generate the integers from a unifrom distribution. Your functions should not set any random number generator seed.

 

b) [6] round_hist(rolls_array):

Given a 2-dimensional Numpy array rolls_array, write a function round_hist() that display a histogram for the sum by columns of the given rolls_array with the following requirements.

• The title is "Histogram of two-dice rolls (i rounds)", where i should be substi- tuted by the number of columns in rolls_array

• The x-axis is labelled as "Sum of the two dice"

• The y-axis is labelled as "Number of occurrences of rolls"

• The bin edges (along the x-axis) range from 2 to 12

For example, if rolls_array is a 2-dimensional Numpy array of [[5, 6, 2, 1, 6, 5, 4, 6, 6, 6],[1, 5, 2, 6, 2, 2, 5, 6, 3, 3]], your function should display

 

 


Hint: You must use the matplotlib pyplot submodule to produce and display the his- togram.

 

c) [6] round_one(win,lose):

Given two tuples win and lose, write a function round_one() that return the outcome (e.g. "win", "lose", or the point) of round one in a game with rolling of two dice.

•  if the sum of two dice rolled is in win, the function return "win"

•  if the sum of two dice rolled is in lose, the function return "lose"

•  if the sum of two dice rolled is neither in win nor in lose, the function return this sum as an integer

Hint: You must call your function roll_dice() to generate the result of rolling two dice. Your function will return an error message if there is overlap between win and lose.

 

d) [6] later_round(point,lose):

Given two integers point and lose, write a function later_round() that return the outcome (e.g.  "win", "lose", or "continue") of later round after round one of a game with rolling of two dice by the following rules

•  if the sum of two dice rolled equals point, the function return "win"

•  if the sum of two dice rolled equals lose, the function return "lose"

•  if the sum of two dice rolled neither equals point nor equals lose, the function return "continue"

Hint: You must call your function roll_dice() to generate the result of rolling two dice. Your function will return an error message if point and lose are equal.

 

e) [10] play_game(round_one_win, round_one_lose,  later_round_lose):

Given two tuples round_one_win and  round_one_lose and an integer later_round_lose, write a function play_game() that simulate playing the two dice game and return the          final outcome and the number of round of this game.

Hint: You must call your functions round_one() and later_round() to generate the result in round one and later rounds. Your function will return an error message if the point is established in round one and later_round_lose are equal.

 

f) [6] game_session(num_game, round_one_win, round_one_lose,  later_round_lose) Assume a play will earn one dollar whenever win this game and pay one dollar when-

ever lose this game. Given two integers num_game, later_round_lose, and two tuples round_one_win,   round_one_lose, write a function game_session() that simulate the two dice gamenum_game times and return a Numpy array with length asnum_game which tracks the profit of this player after each time of the games with the following rules.

•  if the outcome of one game is "win", the profit add one

•  if the outcome of one game is "lose", the profit minus one

•  if the outcome of one game is an error message, the profit keep same


For example, if a simulation of playing 6 games results in the sequence of [win, win,         lose, error message, win, win], then the function should return a Numpy array of [1,2,1,1,2,3] Hint: You must call your function play_game() to generate the result of playing the         two dice game.

 

g) [10]multi_player(num_player, num_game, round_one_win, round_one_lose, later_round_lose):

Given three integers num_player, num_game, later_round_lose, and two tuplesround_one_win, round_one_lose, write a function multi_player()that simulate num_players many

different players, each playing the two dice game num_game times and return the num- ber of players who end up winning more games than they have lost over theirnum_game times playing.  The function also display a single plot where each curve show the changing in the profit of each player. The requirements of the plot is as follows.

• The title is "Nplayers,Mgames played, Pwinners", where N should be substituted by the value ofnum_players, M should be substituted by the value ofnum_game, P should be substituted by the number of players who end up winning more games.

• The x-axis is labelled as Games played

• The y-axis is labelled as Profit

For example, if num_player = 5, num_game = 10, later_round_lose = 7, and tuples round_one_win = (7,11),   round_one_lose = (2,3,12), the function should return 1 and display

 

 

 

Hint: You must call your function game_session() to generate the result of playing the two dice game.You must use the matplotlib pyplot submodule to produce and display the plot