Mike
Who uses C anymore??? Just kidding…
OK. it was C++. I admit it.
I’ve edited the original to use (double) in VM_RANDOM_INT() which works better on some computers.
Same answer.
Here nRed means there is red on at least one side of the card
I’ve updated the program to take into account all card combinations.
If someone can point out what’s wrong with these simulation programs,
then I would be on my way to re-examine my reasoning above.
However, so far, the simulation matches my thinking above.
The chances you initially see a red side is 1/2.
that’s from 1/3 + (1/3)/2 + 0
Similarly for green.
Simulation for 1000 runs produces:
320 = nGrnGrn = 0.32 %
492 = nGrn = 0.492 %
508 = nRed = 0.508 %
338 = nRedRed = 0.338 %
Here, nRed and nGrn means you initially see that color
(different definition than first program).
So the chances you picked a grn/grn card is 1/3
The chances you picked a red/red card is 1/3
Here’s the program.
Unfortunately, this site doesn’t keep the indentation…
#include “stdio.h”
#include “stdlib.h”
#define VM_RANDOM_INT(a,b)
((int)((a)+((b)-(a)+1)*((double)rand()/((double)RAND_MAX+1))))
// card 0 - grn/grn
// card 1 - red/grn
// card 2 - red/red
// side = 1 / 0
int
main(int argc, char **argv)
{
int ii, card, side, nLoop = atoi(argv[1]);
int nGrn = 0, nGrnGrn = 0;
int nRed = 0, nRedRed = 0;
// nRed means the initial side shown is red
// nGrn means the initial side shown is grn
printf("%d = x%X = RAND_MAX
", RAND_MAX, RAND_MAX);
sranddev();
for (ii = 0; ii < nLoop; ii++) {
card = VM_RANDOM_INT(0, 2);
side = VM_RANDOM_INT(0, 1);
switch (card) {
case 0:
nGrn++;
nGrnGrn++; // side doesn't matter
break;
case 1:
if (side) {
nRed++;
} else {
nGrn++;
}
break;
case 2:
nRed++;
nRedRed++; // side doesn't matter
break;
}
printf("%d %d %d) %d %d %d %d
", ii, card, side,
nGrnGrn, nGrn, nRed, nRedRed);
}
printf("%d trials
“, nLoop);
printf(”%d = nGrnGrn = %g %%
“, nGrnGrn, (double)nGrnGrn / nLoop);
printf(”%d = nGrn = %g %%
“, nGrn, (double)nGrn / nLoop);
printf(”%d = nRed = %g %%
“, nRed, (double)nRed / nLoop);
printf(”%d = nRedRed = %g %%
", nRedRed, (double)nRedRed / nLoop);
}