Created
October 1, 2015 09:50
-
-
Save lucidrains/c7d0f30d3289cfae1fa0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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