Author: idle
Blog: blog.csdn.net/cg_ I
Email: B _dx@sohu.com
Reference books: C and pointer
Introduction:
Gao, a big leader, majored in computer science at XXX University in the United States, I wanted to get a diploma in a few years and return to China to eat royal food, I didn't think that foreign masters had to arrange their homework without human feelings. At that time, they were able to hurt the hacker, and suddenly thought that the father on the other side of the ocean had energy. The leaders learned that they had told me to speed up the incident to help ease their grief, hello, I put my hands on it and looked around. I found my eyes shining in the corner. However, I felt so hard that I could not put a fart. Look at the questions and write them at will. Don't you think about them yourself...
Job 2:
"B. (20 points) Simulated Dice
The casino game Table Crapsis a dice game in which a number of betting options are available depending on
The eventual roll of a pair of dice. Players may bet "for" or "against" certain outcomes in a dizzying array
Combinations with varying odds. Each round of Crapsbegins with an initial roll of the dice (the so-called
'Come-out' roll). The player 'WINS 'on the initial roll if it totals 7 or 11, and 'craps out' (loses) if the roll is 2, 3
Or 12.
Write a simulation program to determine the probability of winning vs. losing on the first roll in the game
Craps. Your program must include a void function named diceRollthat will use the rand () function
Provide 2 pseudo-random integer values in the range [1 .. 6] representing a single roll of a pair of (independent)
Dice.
Your mainprogram must do the following:
? Prompt the user, input an integer seed value and seed the pseudo-random number generator with that
Value
? Simulate 10,000 dice rolls using the diceRollfunction and determine the number of 'WINS '(7, 11)
And 'losses '(2, 3, 12)
? Display the number of 'WINS 'and 'losses', and also the resulting probabilities on the terminal display
(Note the theoretical probability for 'win' on the first roll is. 2222 and for a 'loss' on the first roll is. 1111)
Example:
Enter an integer seed value: 1
Number of wins: 2230
Probability of winning: 0.223
Number of losses: 1098
Probability of losing: 0.1098
Enter an integer seed value: 102
Number of wins: 2258
Probability of winning: 0.2258
Number of losses: 1072
Probability of losing: 0.1072
The general idea is to write a game that simulates the dice. Each time two dice are thrown, each dice is a cube with 6 sides marked with 1, 2, 3, 4, 5, and 6 dots, when the dice stop, add the points that each dice faces up. If the sum of the obtained points is 2, 3, or 12, the gamer loses and wins at 7 and 11; simulate the result of throwing the dice for 1000 times."
Code (the job must be implemented in C ++, and I will give it to C if I am not in the mood ):
#include
#include
#define ROLLS 1000void main(){int i = 0;int numOfDice1;int numOfDice2;int sum;int status = 0;int numOfWin = 0;int numOfLose = 0;unsigned value = 0;printf("Enter an integer seed value:");scanf("%u",&value);srand(value);for(i = 0; i <= ROLLS; i++){numOfDice1 = 1 + rand() % 6;numOfDice2 = 1 + rand() % 6;sum = numOfDice1 + numOfDice2;switch(sum){case 7:case 11:status = 1;numOfWin++;break;case 2:case 3:case 12:status = 0;numOfLose++;break;}}printf("Number of winns:%d\n",numOfWin);printf("Probability of winning:%.3f\n",(float)numOfWin / ROLLS);printf("Number of losses:%d\n",numOfLose);printf("Probability of losing:%.3f\n",(float)numOfLose / ROLLS);}
End:
The reason is depressing. However, from the perspective of learning, I hope that you can give me the above Code criticism and suggestions. After all, I am too short to know whether the question is correct. I also hope you can give suggestions. I also learned something from it. The knowledge itself is valuable. I would like to thank you!