辅导program、C++程序语言调试、C++设计讲解
解析C/C++编程|辅导Python程序
Assessment 1: Dice Simulation and the Game of Craps
October 3, 2019
You should prepare a report of the following work for e-submission on Thursday wk5 (25rd of October) at
4pm on Canvas. Include an explanation in words of your programs and the thought processes you went through
in putting them together. Also include listings of your programs and graphs summarising the reults of running your
programs. Each program should be well formatted, indented and commented. You can use Excel to generate the
graphs if you so wish, or you can use any other more advanced graphics program you know (e.g. gnuplot, matlab,
root, R or python’s matplotlib).
1 Using Random Numbers – 20%
You can generate a set of random floating point numbers in the interval [0,1] using the rnd() function in C++. Write
a small program that generates a random sample of a given size N (which is given as input from the command line)
and that computes the mean and the variance of the sample. Use your program to calculate empirically the
lim
N→∞
Var(X) = α
where Var(X) is the variance of the sample of random numbers X. Show that α=1/12.
2 Simulating Dice Throws – 40%
Write a program that simulates the rolling of two dice. Your program should consist of the following functions..
• A function rnd that returns a uniformly distributed random number between 0 and 1 as a double.
• A function dice that uses rnd and returns an int from 1 to 6, thereby simulating the throwing of a six sided
dice.
• A function two dice that simuates throwing two dice and returns the sum of their face values as an int (this
is the score in the next item).
• A main function that simulates a certain number of trials of two dice. It should retrieve the number of trials
as a command-line argument of the program. Keep a tally of the scores in a vector. At the end it should print
out each score and the number of times that score was rolled, one per line with a tab character seperating the
two values.
Save the output of your program to a file called dice N.dat using output redirection and where N is the number
of trials. Get results for 100,500,1000, 10000 and 50000 trials. In your report, provide a suitable table to summarise
your results and a suitable graph to present them visually. Comment on your results.
3 The Game of Craps – 40%
Write a program that simulates a game of craps. You should use your functions from part 1 as part of your solution.
The game proceeds as follows :
• Two dice are thrown and the sum is calculated.
• A score of 2, 3 or 12 immediately loses.
• A score of 7 or 11 immediately wins.
• Any other score becomes the point.
• The player keeps throwing two dice until...
• – The player hits the point value again which wins.
1
– A 7 is thrown which loses
Your program should simulate 10,000 games of craps and keep a tally of how many games were won or lost and
the number of throws needed before winning / losing. Provide suitable tables and graphs of the results to answer the
following questions
• (a) How many games are won on the 1st, 2nd, 3rd...20th roll, and after the 20th roll.
• (b) How many games are lost on the 1st, 2nd, 3rd...20th roll, and after the 20th roll.
• (c) What are the chances of winning at craps ? (include an error estimate)
• (d) What is the average length of a game of craps ? (include an error estimate)
• (e) Do the chances of winning improve with the length of the game ?
2