Skip to content

Instantly share code, notes, and snippets.

@Koronen
Created November 19, 2012 15:46
Show Gist options
  • Save Koronen/4111380 to your computer and use it in GitHub Desktop.
Save Koronen/4111380 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NR_OF_PLAYS (1000000)
#define BET (100)
#define PAYOFF (200)
#define MINIMUM_CERTAINTY (0.75)
#define RED (1)
#define BLACK (2)
#define JOKER (0)
#define CONTINUE (-1)
int random_suit(int reds, int blacks, int jokers)
{
int rand_val = rand() % (reds + blacks + jokers);
if(rand_val < reds)
{
return RED;
}
else if(rand_val < (reds + blacks))
{
return BLACK;
}
else
{
return JOKER;
}
}
int main(int argc, char ** argv)
{
srand(time(NULL));
long long cash = 0;
int playno;
for(playno = 0; playno < NR_OF_PLAYS; ++playno)
{
int reds = 26;
int blacks = 26;
int jokers = 1;
cash -= BET;
while(reds + blacks + jokers > 0)
{
fprintf(stderr, "reds=%2d, blacks=%2d, jokers=%d", reds, blacks, jokers);
int choise = CONTINUE;
double p_red = 1.0 * reds / (reds + blacks + jokers);
fprintf(stderr, ", P(red)=%.3f", p_red);
if(p_red > MINIMUM_CERTAINTY || (reds + blacks + jokers) == 1)
{
choise = RED;
}
int card = random_suit(reds, blacks, jokers);
fprintf(stderr, ", card=%d, choise=%2d\n", card, choise);
if(choise == RED && card == RED)
{
cash += PAYOFF;
break;
}
if(card == RED) --reds;
if(card == BLACK) --blacks;
if(card == JOKER) --jokers;
}
fprintf(stderr, "%39s cash=%lld\n", " ", cash);
}
printf("End balance: %lld\n", cash);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment