#include #include #define TRIM 1 #define TOP 3 #define N 100000 #define TRIES TOP*N int v[TOP]; int my_random(int n); int main(int arch, char **argv) { int i,j; int prize, choose1, choose2, choose, del; int winkeep=0, winchange=0; for( i=0; i < TRIES; i++) { prize=my_random(TOP); choose1=my_random(TOP); if ( prize==choose1 ) { del= (choose1 + 1 ) % TOP; } else if ((( choose1 + 1 ) % TOP ) == prize ) { del = (choose1 + 2 ) % TOP; } else { del= (choose1 + 1 ) % TOP; } choose2 = (choose1 + 1 ) % TOP; if ( choose2 == del ) { choose2 = (choose1 + 1 ) % TOP; } if ( 0 == my_random(2)) { choose=choose2; if ( choose == prize ) { winchange++; } } else { choose=choose1; if ( choose == prize ) { winkeep++; } } printf("%0.2d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",i, prize, choose1, choose2, choose, winchange, winkeep); } return 0; } /* return values so 0 <= x < n */ #if defined(TRIM) int my_random(int n) { int value,aux; if((n<=0)||(n>RAND_MAX)) return(-1); aux= RAND_MAX - ( RAND_MAX % n + 1 ) % n; do value=rand(); while( value > aux); return(value%n); } #elif defined(MOD) int my_random(int n) { return ( rand() % n); } #endif