Skip to content

Instantly share code, notes, and snippets.

@lucidrains
Created October 1, 2015 09:50
Show Gist options
  • Save lucidrains/3ed19d21568d01c61977 to your computer and use it in GitHub Desktop.
Save lucidrains/3ed19d21568d01c61977 to your computer and use it in GitHub Desktop.
var initial_money = 40;
var num_play_times = 30;
var default_bet = 3;
var total_simulations = 1000;
var survives = 0;
var final_money = [];
var flip = function() {
return Math.random() < (18/38);
};
for (var i=0;i<total_simulations;i++){
var survived = true;
var money = initial_money;
var bet = default_bet;
for(var j = 0; j < num_play_times; j++) {
var winning_flip = flip();
if (winning_flip) {
money += bet;
bet = default_bet;
} else {
money -= bet;
bet *= 2;
}
if(money < 0){
survived = false;
break;
}
}
if (survived && money > initial_money) {
survives += 1;
final_money.push(money);
}
};
var average_money_earned = (final_money.reduce(function(s, e){
s += e;
return s;
}, 0) / (final_money.length));
console.log("survived", survives * 100 / total_simulations, "% of the time");
console.log("average money if you survived is $", average_money_earned);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment