Skip to content

Instantly share code, notes, and snippets.

@DonMartin76
Created April 26, 2019 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DonMartin76/97633f634d1c1ba754cd21da9a053a81 to your computer and use it in GitHub Desktop.
Save DonMartin76/97633f634d1c1ba754cd21da9a053a81 to your computer and use it in GitHub Desktop.
Marc Elsberg - Gier - Glücksspiel-Simulation
'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