Giter VIP home page Giter VIP logo

games-dice-loaded's Introduction

Last night I read Keith Schwarz's article about Vose's alias method for simulating rolls of a loaded die. It's worth reading the whole thing, but here's the tl;dr: draw a bar chart of the probabilities of landing on the various sides, then throw darts at it (by picking X and Y coordinates uniformly at random). If you hit a bar with your dart, choose that side. That works OK, but has very bad worst-case behaviour; fortunately, it's possible to cut up the taller bars and stack them on top of the shorter bars in such a way that the area covered is exactly a (1/n) * n rectangle. Constructing this rectangular "dartboard" can be done in O(n) time; thereafter, simulating a die roll can be done in O(1) time (generate the dart's coordinates; which vertical slice did the dart land in, and is it in the shorter bar on the bottom or the "alias" that's been stacked above it?).

"Gosh," I thought, "what an elegant and efficient algorithm! I wonder if anyone has put an implementation of it on CPAN?"

So I looked. And, as far as I can tell, they haven't.

So I wrote this implementation on the bus this morning.

You're welcome.

games-dice-loaded's People

Contributors

arc avatar pozorvlak avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.