/* Metaphysics by Default - Probabilities */ /* copyright 1994-1999 Wayne Stewart */ /* WayneStewart@mbdefault.org */ /* ------------------------------------------------------------------ */ /* This program calculates probabilities for several passage types, */ /* which are defined in Chapter 11 at mbdefault.org. */ /* The program keeps totals of each passage type as it runs through */ /* one million random events. When event total reaches 1 million, it */ /* halts execution, sums the final remaining events, and prints the */ /* aggregate probabilities. Comparison with formal and informal */ /* probability results can be found in Chapter 16 at mbdefault.org */ int redo; int rawB[3277]; int rawD[3277]; int comB[100]; int comD[100]; int B[2]; int D[6]; unsigned int rOne, rTwo, rThree, rFour, rFive, rSix, rtot, rOneTemp; float rOneSum, rTwoSum, rThreeSum, rFourSum, rFiveSum, rSixSum, rTotSum; mainSub () { int i; i=0; /* use i for counting loop when printing compact B&D. Remove when finished. */ redo=1; rOne=0; rTwo=0; rThree=0; rFour=0; rFive=0; rSix=0; rtot=0; rOneTemp=0; while ( redo == 1 ) { redo=0; setupArrays (rawB, rawD); } findRTypes (); } createB (int rawB[]) { int i, j, ranB; i=0; j=0; ranB=0; srand (clock () ); srand (clock () ); srand (clock () ); srand (clock () ); srand (clock () ); for (j=0; j<3277; j++) rawB[j] = 0; for (i=1; i<101; i++) { ranB = rand()/10; if ( rawB[ranB] == 0 ) { rawB[ranB] = ranB; } else { redo=1; } } } createD (int rawD[]) { int i, j, ranD; i=0; j=0; ranD=0; srand (clock () ); srand (clock () ); for (j=0; j<3277; j++) rawD[j] = 0; for (i=1; i<101; i++) { ranD = rand()/10; if ( rawD[ranD] == 0 ) { rawD[ranD] = ranD; } else { redo=1; } } } compact () { int i, j, k, l, m; i=0; j=0; k=0; l=0; m=0; for (m=0; m<100; m++) { comB[m] = 0; comD[m] = 0; } while ( i<100 ) { if ( rawB[j] != 0 ) { comB[i] = rawB[j]; i++; } j++; } while ( k<100 ) { if ( rawD[l] != 0 ) { comD[k] = rawD[l]; k++; } l++; } } setupArrays () { createB (rawB); createD (rawD); compact (rawB, rawD); /* doublecheck the compact arrays. Are they keyed identically? Redo the array if it is a duplicate. */ if ( ( comB[0] == comD[0] ) && ( comB[1] == comD[1] ) ) { redo=1; } } findRTypes () { int i, j, x, y; i=0; j=0; x=0; y=0; for (x=0; x<2; x++) B[x] = 0; for (y=0; y<6; y++) D[y] = 0; while ( comB[i] < comD[0] ) /* get first B after D0 */ i++; j++; while ( comD[j] < comB[i] ) /* get first D after B0 */ j++; while ( comB [i+1] < comD[j] ) /* get a B after D[j] */ i++; B[0] = comB[i]; D[0] = comD[j-1]; rOne = 0; /* Compiler bug: do not use "rOne". */ rOneTemp = 0; /* Use "rOneTemp" instead. */ while ( (i<99) && ((j+5)<99) ) { B[1] = comB[i+1]; D[1] = comD[j]; D[2] = comD[j+1]; D[3] = comD[j+2]; D[4] = comD[j+3]; D[5] = comD[j+4]; D[6] = comD[j+5]; if ( D[2] > B[1] ) { rOneTemp = rOneTemp + 1; rtot = rtot + 1; j++; /* skip births without associated deaths */ while ( ( comB[i+1] < comD[j] ) && ( ( i+1 ) <= 99 ) ) { i++; if ( ( i+1 ) >= 99 ) break; } } else { if ( D[3] > B[1] ) { rTwo = rTwo + 1; rtot = rtot + 1; j = j + 2; /* skip births without associated deaths */ while ( ( comB[i+1] < comD[j] ) && ( ( i+1 ) <= 99 ) ) { i++; if ( ( i+1 ) >= 99 ) break; } } else { if ( D[4] > B[1] ) { rThree = rThree + 1; rtot = rtot + 1; j = j + 3; /* skip births without associated deaths */ while ( ( comB[i+1] < comD[j] ) && ( ( i+1 ) <= 99 ) ) { i++; if ( ( i+1 ) >= 99 ) break; } } else { if ( D[5] > B[1] ) { rFour = rFour + 1; rtot = rtot + 1; j = j + 4; /* skip births without associated deaths */ while ( ( comB[i+1] < comD[j] ) && ( ( i+1 ) <= 99 ) ) { i++; if ( ( i+1 ) >= 99 ) break; } } else { if ( D[6] > B[1] ) { rFive = rFive + 1; rtot = rtot + 1; j = j + 5; /* skip births without associated deaths */ while ( ( comB[i+1] < comD[j] ) && ( ( i+1 ) <= 99 ) ) { i++; if ( ( i+1 ) >= 99 ) break; } } else { /* skip next deaths before next birth, till element 99 */ rSix = rSix + 1; rtot = rtot + 1; while ( ( comD[j] < B[1] ) && ( j < 99 ) ) { j++; if ( j == 99 ) break; } /* skip births without associated deaths */ while ( ( comB[i+1] < comD[j] ) && ( ( i+1 ) <= 99 ) ) { i++; if ( ( i+1 ) >= 99 ) break; } } } } } } } } stats () { float rOnePer, rTwoPer, rThreePer, rFourPer, rFivePer, rSixPer, rMulPer; rOnePer=0.0; rTwoPer=0.0; rThreePer=0.0; rFourPer=0.0; rFivePer=0.0; /* Here we calculate the percentage probabilities for 1-to-1, 2-to-1, */ /* 3-to-1, 4-to-1 and 5-to-1 passage types. Note that each is */ /* multiplied by the number of passage participants, and divided by a */ /* normalizing constant ("2" in this case), in order to obtain the absolute */ /* experienced probabilities for each passage type. */ rOnePer = 0.5*1*rOneSum*100/rTotSum; printf ("One-to-one percentage is %.2f\n", rOnePer); rTwoPer = 0.5*2*rTwoSum*100/rTotSum; printf ("Two-to-one percentage is %.2f\n", rTwoPer); rThreePer = 0.5*3*rThreeSum*100/rTotSum; printf ("Three-to-one percentage is %.2f\n", rThreePer); rFourPer = 0.5*4*rFourSum*100/rTotSum; printf ("Four-to-one percentage is %.2f\n", rFourPer); rFivePer = 0.5*5*rFiveSum*100/rTotSum; printf ("Five-to-one percentage is %.2f\n", rFivePer); } main () { rOneSum=0; rTwoSum=0; rThreeSum=0; rFourSum=0; rFiveSum=0; rSixSum=0; rTotSum=0; rOne=0; rOneTemp=0; rTwo=0; rThree=0; rFour=0; rFive=0; rSix=0; rtot=0; while ( rTotSum < 1000000 ) { rTotSum = ( rTotSum + rtot); rOneSum = ( rOneSum + rOneTemp); rTwoSum = ( rTwoSum + rTwo); rThreeSum = (rThreeSum + rThree); rFourSum = (rFourSum + rFour); rFiveSum = (rFiveSum + rFive); rSixSum = (rSixSum + rSix); printf ("TotalSum = %.0f\n", rTotSum); printf ("OneSum = %.0f\n", rOneSum); printf ("TwoSum = %.0f\n", rTwoSum); printf ("ThreeSum = %.0f\n", rThreeSum); printf ("FourSum = %.0f\n", rFourSum); printf ("FiveSum = %.0f\n", rFiveSum); printf ("Six+Sum = %.0f\n\n\n", rSixSum); mainSub(); } stats (); }