https://github.com/0tickpulse/coupon-simulator
https://github.com/0tickpulse/coupon-simulator
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/0tickpulse/coupon-simulator
- Owner: 0tickpulse
- Created: 2023-09-16T08:50:48.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-09-16T08:58:09.000Z (almost 3 years ago)
- Last Synced: 2025-03-18T00:44:45.087Z (about 1 year ago)
- Language: Rust
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Coupon Simulator
This is a simulation of the famous [coupon collector's problem](https://en.wikipedia.org/wiki/Coupon_collector%27s_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](https://en.wikipedia.org/wiki/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
```bash
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.