Code Monkey home page Code Monkey logo

coupon-simulator's Introduction

Coupon Simulator

This is a simulation of the famous coupon collector's problem. The problem is as follows:

Given a set of $n$ coupons, each with a unique number, how many coupons do you need to draw with replacement until you have drawn each coupon at least once?

The answer is $nH_n$, where $H_n$ is the $n$ th harmonic number. This simulation will run the experiment for a given number of coupons and write the average number of draws to the standard output.

Usage

cargo run

Explanation

The mathematical simulation lies in the trial function. The function creates an array called collected, which has N elements, each being false. Each element in this array represents whether that element has been collected. While any of the elements are false, it will keep generating a random number between 0 and N and setting the corresponding element in collected to true. Once all elements are true, it will return the number of draws it took to collect all coupons, completing one trial of the simulation.

The rest of the program is just a loop that creates a few threads and runs the trial function in each thread. It then takes the average of the results and prints it to the standard output.

coupon-simulator's People

Watchers

 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.