Monte Carlo simulator for real money games
JavaScript
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
test
.eslintrc
.gitignore
.travis.yml
LICENSE
README.md
example.js
index.js
package.json

README.md

Monte Carlo simulator for real money games

Build Status Dependency Status devDependency Status

This is a oppinonated framework for calculating chances of outcomes for games based on a simulation. This can be used to model rules and paytables a test them for win rates and average player return.

It's especially useful for games that combine chance elements with skill (like quizzes) and real money gambling and have rules that make it hard to work out the results mathematically.

Uses ES6 and is therefore Node >4.0.0 only.

Features

  • Optimised for speed
  • Simulates different skill levels (By default 50%, 75% and 100% correct)
  • Build-in support for collections and presenting results
  • Progress indicator
  • Exposed RNG operations (randomDouble(), random(min, max) and shuffle(list)) based on Mersenne Twister for better randomness

Example

What's the chance of answering N questions correctly in a row if there's a 5% chance of hitting a question that's impossible to answer?

#!/usr/bin/env node
"use strict";

let MonteCarlo = require("./index.js"); // Use "monte-carlo" outside this repo
let _ = require("underscore");

class ImpossibleQuizSimulator extends MonteCarlo.Simulator {
    before(results) {
        results.add("wins", new MonteCarlo.Results.Counter());
        results.add("payout", new MonteCarlo.Results.PayoutStandardDeviationCounter());
    }

    createGameState(rules) {
        let questions = _.map(_.range(rules.questions), () => {
            return {possible: this.randomDouble() > rules.chanceOfImpossibleQuestion};
        });

        return {
            questions: questions
        }
    }

    game(rules, gameState, results, skillOutcome) {
        let lost = false;

        while (!lost && gameState.questions.length) {
            let question = gameState.questions.pop();
            if (!question.possible || !skillOutcome()) {
                lost = true;
            }
        }

        if (!lost) {
            if (rules.amountsToBeWon) {
                results.payout.increase(this.shuffle(rules.amountsToBeWon)[0]);
            } else {
                results.payout.increase(this.random(rules.amountToBeWonMin, rules.amountToBeWonMax));
            }
            results.wins.increase();
        }
    }
}

let simulator = new ImpossibleQuizSimulator({ N: 10000 });

simulator.run("10 questions", {
    chanceOfImpossibleQuestion: 0.05,
    questions: 10,
    amountToBeWonMin: 50,
    amountToBeWonMax: 100
});

simulator.run("8 questions", {
    chanceOfImpossibleQuestion: 0.05,
    questions: 8,
    amountToBeWonMin: 25,
    amountToBeWonMax: 50
});

simulator.run("8 questions, fixed amounts", {
    chanceOfImpossibleQuestion: 0.05,
    questions: 8,
    amountsToBeWon: [1, 10, 25, 50, 100]
});

Result:

==== 10 questions ====
N = 10000
speed: 8475games/sec, total 1.18sec

== Skill Level: 100% ==

wins: 6014 (60.14%) - 1 in 1.66
payout: £45.15 per game, £75.07 per won game (6014 won games), total of £451483.00 (STDDEV: £34.88)

== Skill Level: 75% ==

wins: 318 (3.18%) - 1 in 31.45
payout: £2.42 per game, £75.95 per won game (318 won games), total of £24151.00 (STDDEV: £18.65)

== Skill Level: 50% ==

wins: 3 (0.03%) - 1 in 3333.33
payout: £0.02 per game, £67.00 per won game (3 won games), total of £201.00 (STDDEV: £1.68)


==== 8 questions ====
N = 10000
speed: 13316games/sec, total 0.751sec

== Skill Level: 100% ==

wins: 6534 (65.34%) - 1 in 1.53
payout: £24.44 per game, £37.40 per won game (6534 won games), total of £244376.00 (STDDEV: £16.65)

== Skill Level: 75% ==

wins: 647 (6.47%) - 1 in 15.46
payout: £2.45 per game, £37.92 per won game (647 won games), total of £24535.00 (STDDEV: £12.68)

== Skill Level: 50% ==

wins: 30 (0.30%) - 1 in 333.33
payout: £0.11 per game, £38.03 per won game (30 won games), total of £1141.00 (STDDEV: £2.99)


==== 8 questions, fixed amounts ====
N = 10000
speed: 14472games/sec, total 0.691sec

== Skill Level: 100% ==

wins: 6655 (66.55%) - 1 in 1.50
payout: £25.24 per game, £37.92 per won game (6655 won games), total of £252381.00 (STDDEV: £35.35)

== Skill Level: 75% ==

wins: 689 (6.89%) - 1 in 14.51
payout: £2.56 per game, £37.16 per won game (689 won games), total of £25602.00 (STDDEV: £17.83)

== Skill Level: 50% ==

wins: 26 (0.26%) - 1 in 384.62
payout: £0.08 per game, £30.58 per won game (26 won games), total of £795.00 (STDDEV: £2.92)

In other words: The game tested isn't a great real money game.