1
0
Fork 0

Problem 84 - improved random number for dice, but it's still wrong :(

This commit is contained in:
Jonathan Chan 2018-08-24 21:23:37 -07:00
parent 9f101e40d4
commit c50425679e
2 changed files with 82 additions and 90 deletions

BIN
84

Binary file not shown.

24
84.c
View File

@ -2,6 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
const int rolls = 40000000;
const int GO = 0, JAIL = 10, G2J = 30; // GO/JAIL const int GO = 0, JAIL = 10, G2J = 30; // GO/JAIL
const int CC1 = 2, CC2 = 17, CC3 = 33; // Community Chest const int CC1 = 2, CC2 = 17, CC3 = 33; // Community Chest
const int CH1 = 7, CH2 = 22, CH3 = 36; // Chance const int CH1 = 7, CH2 = 22, CH3 = 36; // Chance
@ -10,10 +11,8 @@ const int U1 = 12, U2 = 28; // Utilities
const int C1 = 11, E3 = 24, H2 = 39; // other const int C1 = 11, E3 = 24, H2 = 39; // other
int freq[40] = {0}; int freq[40] = {0};
int doubleRolls = 0; int doubleRolls = 0, position = 0;
int position = GO; int cci = 0, chi = 0;
int cci = 0;
int chi = 0;
int chance() { int chance() {
chi = (chi + 1) % 16; chi = (chi + 1) % 16;
@ -30,8 +29,7 @@ int chance() {
if (position == CH2) return R3; if (position == CH2) return R3;
return R1; return R1;
case 9: case 9:
if (position == CH2) return U2; return (position == CH2) ? U2 : U1;
return U1;
case 10: position -= 3; case 10: position -= 3;
default: return position; default: return position;
} }
@ -49,9 +47,9 @@ int communityChest() {
int main(void) { int main(void) {
srand(time(NULL)); srand(time(NULL));
for (int i = 0; i < 40000000; i++) { for (int i = 0; i < rolls; i++) {
int die1 = (rand() % 6) + 1; int die1 = rand() / ((double) RAND_MAX + 1) * 6 + 1;
int die2 = (rand() % 6) + 1; int die2 = rand() / ((double) RAND_MAX + 1) * 6 + 1;
position = (position + die1 + die2) % 40; position = (position + die1 + die2) % 40;
if (die1 == die2) { if (die1 == die2) {
doubleRolls = (doubleRolls + 1) % 3; doubleRolls = (doubleRolls + 1) % 3;
@ -71,21 +69,15 @@ int main(void) {
freq[position]++; freq[position]++;
} }
float sum = 0;
for (int i = 0; i < 40; i++) {
sum += freq[i];
}
for (int i = 1; i <= 3; i++) { for (int i = 1; i <= 3; i++) {
int topSquare = 0, topValue = 0; int topSquare = 0, topValue = 0;
float topFreq = 0;
for (int j = 0; j < 40; j++) { for (int j = 0; j < 40; j++) {
if (freq[j] > topValue) { if (freq[j] > topValue) {
topSquare = j; topSquare = j;
topValue = freq[j]; topValue = freq[j];
topFreq = topValue / sum;
freq[j] = 0; freq[j] = 0;
} }
} }
printf("#%d: %d @ %f%%\n", i, topSquare, topFreq * 100); printf("#%d: %d @ %f%%\n", i, topSquare, (float) topValue / rolls * 100);
} }
} }