Giter Club home page Giter Club logo

busraffle's Introduction

n-item Raffle Simulation using "Algorithm R" aka. "Crazy Bus Driver Algorithm"

This script is useful for running an raffle where you are giving away n identical items to an unknown number p participants where n <= p. Each participant will have a equal probability of having one of the items by the end of the simulation.

Mathematically, this relies on "Algorithm R" for reservoir sampling:

procedure Sample(population, n) (
    ฮ” A is an array of size n
    Initialize A to the first n elements from population
    j <- n
    for each of the remaining elements in the population as e (
        j <- j + 1
        if Bernoulli(n / j) (
            Choose a random element from A and swap it with e
        )
    )
    return A
)

A proof that this algorithm creates a random sample can be found here.

Here is a sample run of the script:

What are you giving away? shirts
How many shirts are you giving away? 10
Do you want to enter names? (y/N) n
To start, participants 0 through 9 get one of the shirts (for now...)

List anyone who was gone, end with a blank line...
4
Participant 10 gets one of the shirts (for now) then.

Continue with participant 11 (Y/n)? y
Nothing happened.
Continue with participant 12 (Y/n)? y
Participant 12 steals one of the shirts from participant 6!
...
Continue with particpant 54 (Y/n)? n
Quit entirely (y/N)? y

Winners are: 0, 14, 3, 48, 22, 35, 50, and 25!

busraffle's People

Contributors

jackrosenthal avatar josephmckinsey 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.