radman:
The odds that the krugerrand is under the ashtray I chose first is always 1:3 or 33.333%.
Unless I made a coding mistake, Monte Carlo says you have that backwards correct...
State.h
#ifndef States_h
#define States_h
typedef enum
{
sStart = 0b00000000,
sKrugerrand = 0b00000001,
sBarmanFlipped = 0b00000010,
sFirstGuess = 0b00000100,
sSecondGuess = 0b00001000,
sChangedMind = 0b00010000,
}
state_t;
#endif
Krugerrand.ino
#include "States.h"
void setup( void )
{
Serial.begin( 115200 );
}
static void SetState( state_t & set, state_t element )
{
set = (state_t)(set | element);
}
static void PrintState( state_t set )
{
if ( set & sKrugerrand ) Serial.write('K'); else Serial.write(' ');
if ( set & sBarmanFlipped ) Serial.write('B'); else Serial.write(' ');
if ( set & sFirstGuess ) Serial.write('1'); else Serial.write(' ');
if ( set & sSecondGuess ) Serial.write('2'); else Serial.write(' ');
if ( set & sChangedMind ) Serial.write('C'); else Serial.write(' ');
}
static uint32_t Same;
static uint32_t Change;
static uint32_t Total;
static uint32_t Paydirt;
void loop( void )
{
state_t Ashtray[3];
uint8_t FirstGuess;
uint8_t SecondGuess;
uint8_t i;
Ashtray[0] = sStart;
Ashtray[1] = sStart;
Ashtray[2] = sStart;
SetState( Ashtray[random(0,3)], sKrugerrand );
FirstGuess = random(0,3);
SetState( Ashtray[FirstGuess], sFirstGuess );
do
{
i = random(0,3);
}
while ( Ashtray[i] != sStart );
SetState( Ashtray[i], sBarmanFlipped );
/*
if ( random(0,2) == 0 )
{
++Same;
SecondGuess = FirstGuess;
SetState( Ashtray[SecondGuess], sSecondGuess );
}
else
*/
{
++Change;
for ( int8_t i=0; i < 3; ++i )
{
if ( (Ashtray[i] & (sBarmanFlipped | sFirstGuess)) == 0 )
{
SecondGuess = i;
SetState( Ashtray[SecondGuess], sSecondGuess );
SetState( Ashtray[SecondGuess], sChangedMind );
break;
}
}
}
for ( int8_t i=0; i < 3; ++i )
{
if ( (Ashtray[i] & (sKrugerrand | sSecondGuess | sChangedMind)) == (sKrugerrand | sSecondGuess | sChangedMind) )
{
++Paydirt;
break;
}
}
++Total;
/*
for ( int8_t i=0; i < 3; ++i )
{
PrintState( Ashtray[i] );
Serial.write( '\t' );
}
*/
if ( (Total & 0x000000FF) == 0 )
{
/*
Serial.print( (Same * 1000) / Total );
Serial.write( '\t' );
*/
Serial.print( (Paydirt * 1000) / Total );
Serial.println();
}
}