Last active
December 24, 2016 06:48
-
-
Save JanneSalokoski/2bd202bd432dd08867ea3a5095537458 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Game = function (return_function) | |
{ | |
this.play = function (bet, callback) | |
{ | |
var return_amount = return_function(bet) | |
if (callback !== undefined) | |
{ | |
callback(return_sum); | |
} | |
return return_amount; | |
} | |
} | |
function random_return(bet) | |
{ | |
var rand = Math.random(); | |
if (rand <= 0.50) | |
{ | |
return 0; | |
} | |
else if (rand <= 0.75) | |
{ | |
return bet / 2; | |
} | |
else if (rand <= 0.875) | |
{ | |
return bet; | |
} | |
else if (rand <= 0.9375) | |
{ | |
return bet * 2; | |
} | |
else if (rand <= 0.975) | |
{ | |
return bet * 5; | |
} | |
else if (rand <= 0.96875) | |
{ | |
return bet * 10; | |
} | |
else if (rand <= 1) | |
{ | |
return bet * 25; | |
} | |
} | |
var game = new Game(random_return); | |
function play_game() | |
{ | |
var credit = 2.00; | |
for (var i = 0; i < 100; i++) | |
{ | |
if (credit <= 0.10 && credit > 0) | |
{ | |
credit = Math.round(Math.random()) ? 0 : 0.20 | |
} | |
if (credit > 0) | |
{ | |
var win = game.play(0.20); | |
// console.log("Round " + (i+1) + ": " + credit + "€ - 0.20€ + " + win + "€ = " + (credit - 0.20 + win).toFixed(2) + "€"); | |
credit -= 0.20; | |
credit += win; | |
credit = credit.toFixed(2); | |
} | |
else | |
{ | |
break; | |
} | |
} | |
// console.log("Won: " + (credit - 2).toFixed(2) + "€"); | |
return (credit - 2).toFixed(2); | |
} | |
function test() | |
{ | |
var rounds = []; | |
for (var i = 0; i < 100000; i++) | |
{ | |
rounds.push(play_game()); | |
} | |
var sum = 0; | |
for (var win of rounds) | |
{ | |
sum += Number(win); | |
} | |
average = sum / rounds.length; | |
var return_percentage = sum / (2 * rounds.length); | |
console.log("\nStatistics:"); | |
console.log("\nIncome: " + (2 * rounds.length) + "€"); | |
console.log("Expense: " + sum.toFixed(2) + "€"); | |
console.log("\nAverage win: " + average.toFixed(2) + "€"); | |
console.log("Return rate: " + (return_percentage * 100).toFixed(2) + "%"); | |
} | |
test(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment