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.

172
84.c
View File

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