[ create a new paste ] login | about

Link: http://codepad.org/WbZAEXA4    [ raw code | output | fork ]

C++, pasted on May 6:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdint.h>

typedef struct{
    double winrate;
    int stars;
    int streak;
}Player;

inline double uniform(void){
    return (double)rand() / RAND_MAX;
}

inline void play_game(Player* player){
    int isWin = uniform() < player->winrate;
    if(isWin){
        ++player->streak;
        ++player->stars;
        if(player->stars <= 70 && player->streak >= 3) ++player->stars;
    }
    else{
        player->streak = 0;
        if(player->stars > 10) --player->stars;
    }
}

int main(int argc, char* argv[]){   

    double winrate = 0.6;
    int64_t nTrials = 100000;

    srand(time(NULL));    
 
    int64_t nGames = 0;
    for(int64_t i = 0; i < nTrials; ++i){
        Player player = {winrate, 0, 0};
        while(player.stars < 96){
            play_game(&player);
            ++nGames;
        }
    }
    printf("%f\n", (double)nGames / nTrials);

    return 0;
}


Output:
1
280.979720


Create a new paste based on this one


Comments: