npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

monte-carlo

v1.2.0

Published

Monte Carlo simulator for real money games

Downloads

7

Readme

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.