Created
April 26, 2019 12:49
Star
You must be signed in to star a gist
Marc Elsberg - Gier - Glücksspiel-Simulation
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
'use strict'; | |
// const BUCKETS = [0, 100, 500, 1000, 1500, 2000, 2500, 3000, 4500, 5000, 13150]; | |
const ROUNDS = 100; | |
const ITERATIONS = 10000000.0; | |
const BUCKETS = [0, 1, 2.5, 5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000, 13150]; | |
function initBuckets() { | |
const buckets = {}; | |
for (let b of BUCKETS) { | |
buckets[b] = 0; | |
} | |
buckets.Inf = 0; | |
return buckets; | |
} | |
function addToBucket(buckets, value) { | |
let isInf = true; | |
for (let b in buckets) { | |
if (b === 'Inf') | |
continue; | |
if (value < b) { | |
buckets[b]++; | |
isInf = false; | |
} | |
} | |
if (isInf) | |
buckets.Inf++; | |
} | |
function playGame() { | |
let value = 100.0; | |
for (let i = 0; i < ROUNDS; ++i) { | |
if (Math.random() < 0.5) | |
value *= 0.6; | |
else | |
value *= 1.5; | |
} | |
return value; | |
} | |
let sum = 0; | |
let max = 0; | |
const buckets = initBuckets(); | |
for (let i = 0; i < ITERATIONS; ++i) { | |
const res = playGame(); | |
addToBucket(buckets, res); | |
sum += res; | |
if (res > max) | |
max = res; | |
} | |
console.log(`Mean of ${ITERATIONS} iterations: ${sum / ITERATIONS}`); | |
//console.log(`Expected: ${100.0 * Math.pow(1.05, ROUNDS)}`); | |
console.log(`Maximum value: ${max.toExponential(2)}`); | |
console.log(); | |
for (let b in buckets) { | |
console.log(`Values under ${b}: ${buckets[b]}`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment