两个骰子滚动模拟器
Two dice rolling simulator
#pragma warning(disable:4996)
// INCLUDES
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// DEFINES
#define LOWER_DIE 1 // LOWER_DIE must be at least 1
#define UPPER_DIE 10
#define TRIALS 10000
// PROTOTYPES
int genRand(int, int);
int getTwoDieSum(int, int);
// MAIN
int main( int total) {
int numberArray[2 * UPPER_DIE + 1] = { '0', '0', '0', '0', '0', '0', '0','0','0','0','0', '0', '0' };// holds frequency of rolls
int roll = total;
int i = 0;
srand((unsigned int)time(NULL));
for (i = 0; i < TRIALS; i++) {
getTwoDieSum( LOWER_DIE, UPPER_DIE);
numberArray[roll] == numberArray[roll] + 1;
}
printf("ROLL VALUE : FREQUENCY\n");
printf("2\t: %d\n", numberArray[2]);
printf("3\t: %d\n", numberArray[3]);
printf("4\t: %d\n", numberArray[4]);
printf("5\t: %d\n", numberArray[5]);
printf("6\t: %d\n", numberArray[6]);
printf("7\t: %d\n", numberArray[7]);
printf("8\t: %d\n", numberArray[8]);
printf("9\t: %d\n", numberArray[9]);
printf("10\t: %d\n", numberArray[10]);
printf("11\t: %d\n", numberArray[11]);
printf("12\t: %d\n", numberArray[12]);
return 0;
}
// FUNCTION IMPLEMENTATIONS
int genRand(int lower, int upper) {
int range = (upper - lower) + 1;
return rand() % range + lower;
}
int getTwoDieSum( int lower, int upper) {
int rollOne = 0;
int rollTwo = 0;
int total = 0;
rollOne = genRand(lower, upper);
rollTwo = genRand(lower, upper);
total = rollOne + rollTwo;
return total;
}
到目前为止,我所拥有的应该是生成随机数,然后填充数组的正确元素。为所有 printf() 语句保持输出 48。我搞砸的任何想法?我认为它在 genRand() 区域或 getTwoDieSum() 中。我需要使用以下 shell,我不太确定应该做什么,这就是我搞砸的原因。另外我不太确定 int lower 和 int upper 来自哪里:
int genRand(int lower, int upper) {
int range = (upper - lower) + 1;
return rand() % range + lower;
}
我的问题更多是我哪里搞砸了,int lower 和 int upper 来自哪里,我是否正确使用了随机种子生成器?
Shell需要使用:
// INCLUDES
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// DEFINES
#define LOWER_DIE 1 // LOWER_DIE must be at least 1
#define UPPER_DIE 10
#define TRIALS 10000
// PROTOTYPES
int genRand(int, int);
int getTwoDieSum();
// MAIN
int main() {
int numberArray[2 * UPPER_DIE + 1]; // holds frequency of rolls
// initialize numberArray
// seed the random number generator
// roll the dice and keep track of what was rolled in the array
// display results
return 0;
}
// FUNCTION IMPLEMENTATIONS
int genRand(int lower, int upper) {
int range = (upper - lower) + 1;
return rand() % range + lower;
}
int getTwoDieSum() {
// YOUR IMPLEMENTATION GOES HERE
通过使用:
int main(void) {
int numberArray[2 * UPPER_DIE + 1] = { '0', '0', '0', '0', '0', '0', '0','0','0','0','0', '0', '0' };// holds frequency of rolls
int roll = 0;
int i = 0;
srand((unsigned int)time(NULL));
for (i = 0; i < TRIALS; i++) {
getTwoDieSum( LOWER_DIE, UPPER_DIE);
int main_total = getTwoDieSum( LOWER_DIE, UPPER_DIE);
roll = main_total;
numberArray[roll] = numberArray[roll] + 1;
}
我能够让它正常工作。
您可以进一步改进您的代码。例如,计算两个掷骰总和的函数可以简化为:
int getTwoDieSum( int lower, int upper ) {
return genRand(lower, upper) + genRand(lower, upper);
}
现在,作为getTwoDieSum
returns一个值,就像genRand
一样,你最好使用它。您的 main
函数可能如下所示:
// MAIN
int main() {
// holds frequency of rolls
int numberArray[2 * UPPER_DIE + 1] = {0};
// The size of the array of frequency is set to 21, so that numberArray[20]
// represents the frequency of roll 10+10.
// The first two elements (numberArray[0] and numberArray[1]) are unused
int size = sizeof(numberArray) / sizeof(int);
int i;
// If an array is partially initialized, elements that are not initialized
// receive the value 0 of the appropriate type. Alternatively you can use:
//
// for ( i = 0; i < size; ++i ) {
// numberArray[i] = 0;
// }
//
// or simply
//
// memset(numberArray,0,size);
// seed the random number generator
srand(time(NULL));
// roll the dice and keep track of what was rolled in the array
int roll;
for (i = 0; i < TRIALS; i++) {
roll = getTwoDieSum( LOWER_DIE, UPPER_DIE);
++numberArray[roll];
}
// display results
printf("ROLL VALUE : FREQUENCY\n");
for (i = 2; i < size; i++) {
// numberArray[2] represent the frequency of roll 1+1,
// while numberArray[20] is the frequency of roll 10+10
printf("%d\t: %d\n", i, numberArray[i]);
// add ^^ the roll value
}
return 0;
}
一个典型的 运行 给出这个输出:
ROLL VALUE : FREQUENCY
2 : 99
3 : 179
4 : 273
5 : 390
6 : 513
7 : 587
8 : 678
9 : 777
10 : 902
11 : 1039
12 : 913
13 : 822
14 : 672
15 : 614
16 : 514
17 : 433
18 : 298
19 : 195
20 : 102
#pragma warning(disable:4996)
// INCLUDES
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// DEFINES
#define LOWER_DIE 1 // LOWER_DIE must be at least 1
#define UPPER_DIE 10
#define TRIALS 10000
// PROTOTYPES
int genRand(int, int);
int getTwoDieSum(int, int);
// MAIN
int main( int total) {
int numberArray[2 * UPPER_DIE + 1] = { '0', '0', '0', '0', '0', '0', '0','0','0','0','0', '0', '0' };// holds frequency of rolls
int roll = total;
int i = 0;
srand((unsigned int)time(NULL));
for (i = 0; i < TRIALS; i++) {
getTwoDieSum( LOWER_DIE, UPPER_DIE);
numberArray[roll] == numberArray[roll] + 1;
}
printf("ROLL VALUE : FREQUENCY\n");
printf("2\t: %d\n", numberArray[2]);
printf("3\t: %d\n", numberArray[3]);
printf("4\t: %d\n", numberArray[4]);
printf("5\t: %d\n", numberArray[5]);
printf("6\t: %d\n", numberArray[6]);
printf("7\t: %d\n", numberArray[7]);
printf("8\t: %d\n", numberArray[8]);
printf("9\t: %d\n", numberArray[9]);
printf("10\t: %d\n", numberArray[10]);
printf("11\t: %d\n", numberArray[11]);
printf("12\t: %d\n", numberArray[12]);
return 0;
}
// FUNCTION IMPLEMENTATIONS
int genRand(int lower, int upper) {
int range = (upper - lower) + 1;
return rand() % range + lower;
}
int getTwoDieSum( int lower, int upper) {
int rollOne = 0;
int rollTwo = 0;
int total = 0;
rollOne = genRand(lower, upper);
rollTwo = genRand(lower, upper);
total = rollOne + rollTwo;
return total;
}
到目前为止,我所拥有的应该是生成随机数,然后填充数组的正确元素。为所有 printf() 语句保持输出 48。我搞砸的任何想法?我认为它在 genRand() 区域或 getTwoDieSum() 中。我需要使用以下 shell,我不太确定应该做什么,这就是我搞砸的原因。另外我不太确定 int lower 和 int upper 来自哪里:
int genRand(int lower, int upper) {
int range = (upper - lower) + 1;
return rand() % range + lower;
}
我的问题更多是我哪里搞砸了,int lower 和 int upper 来自哪里,我是否正确使用了随机种子生成器?
Shell需要使用:
// INCLUDES
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// DEFINES
#define LOWER_DIE 1 // LOWER_DIE must be at least 1
#define UPPER_DIE 10
#define TRIALS 10000
// PROTOTYPES
int genRand(int, int);
int getTwoDieSum();
// MAIN
int main() {
int numberArray[2 * UPPER_DIE + 1]; // holds frequency of rolls
// initialize numberArray
// seed the random number generator
// roll the dice and keep track of what was rolled in the array
// display results
return 0;
}
// FUNCTION IMPLEMENTATIONS
int genRand(int lower, int upper) {
int range = (upper - lower) + 1;
return rand() % range + lower;
}
int getTwoDieSum() {
// YOUR IMPLEMENTATION GOES HERE
通过使用:
int main(void) {
int numberArray[2 * UPPER_DIE + 1] = { '0', '0', '0', '0', '0', '0', '0','0','0','0','0', '0', '0' };// holds frequency of rolls
int roll = 0;
int i = 0;
srand((unsigned int)time(NULL));
for (i = 0; i < TRIALS; i++) {
getTwoDieSum( LOWER_DIE, UPPER_DIE);
int main_total = getTwoDieSum( LOWER_DIE, UPPER_DIE);
roll = main_total;
numberArray[roll] = numberArray[roll] + 1;
}
我能够让它正常工作。
您可以进一步改进您的代码。例如,计算两个掷骰总和的函数可以简化为:
int getTwoDieSum( int lower, int upper ) {
return genRand(lower, upper) + genRand(lower, upper);
}
现在,作为getTwoDieSum
returns一个值,就像genRand
一样,你最好使用它。您的 main
函数可能如下所示:
// MAIN
int main() {
// holds frequency of rolls
int numberArray[2 * UPPER_DIE + 1] = {0};
// The size of the array of frequency is set to 21, so that numberArray[20]
// represents the frequency of roll 10+10.
// The first two elements (numberArray[0] and numberArray[1]) are unused
int size = sizeof(numberArray) / sizeof(int);
int i;
// If an array is partially initialized, elements that are not initialized
// receive the value 0 of the appropriate type. Alternatively you can use:
//
// for ( i = 0; i < size; ++i ) {
// numberArray[i] = 0;
// }
//
// or simply
//
// memset(numberArray,0,size);
// seed the random number generator
srand(time(NULL));
// roll the dice and keep track of what was rolled in the array
int roll;
for (i = 0; i < TRIALS; i++) {
roll = getTwoDieSum( LOWER_DIE, UPPER_DIE);
++numberArray[roll];
}
// display results
printf("ROLL VALUE : FREQUENCY\n");
for (i = 2; i < size; i++) {
// numberArray[2] represent the frequency of roll 1+1,
// while numberArray[20] is the frequency of roll 10+10
printf("%d\t: %d\n", i, numberArray[i]);
// add ^^ the roll value
}
return 0;
}
一个典型的 运行 给出这个输出:
ROLL VALUE : FREQUENCY
2 : 99
3 : 179
4 : 273
5 : 390
6 : 513
7 : 587
8 : 678
9 : 777
10 : 902
11 : 1039
12 : 913
13 : 822
14 : 672
15 : 614
16 : 514
17 : 433
18 : 298
19 : 195
20 : 102