{"id":27334818,"url":"https://github.com/dmuth/monte-carlo-in-rust","last_synced_at":"2025-04-12T14:46:24.803Z","repository":{"id":265984802,"uuid":"877018417","full_name":"dmuth/monte-carlo-in-rust","owner":"dmuth","description":null,"archived":false,"fork":false,"pushed_at":"2024-12-12T03:28:29.000Z","size":125,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-06T02:35:21.304Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dmuth.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-23T00:15:31.000Z","updated_at":"2024-12-25T09:28:06.000Z","dependencies_parsed_at":"2024-12-01T23:39:54.285Z","dependency_job_id":null,"html_url":"https://github.com/dmuth/monte-carlo-in-rust","commit_stats":null,"previous_names":["dmuth/monte-carlo-in-rust"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmuth%2Fmonte-carlo-in-rust","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmuth%2Fmonte-carlo-in-rust/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmuth%2Fmonte-carlo-in-rust/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmuth%2Fmonte-carlo-in-rust/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dmuth","download_url":"https://codeload.github.com/dmuth/monte-carlo-in-rust/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248585212,"owners_count":21128966,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-04-12T14:46:24.192Z","updated_at":"2025-04-12T14:46:24.796Z","avatar_url":"https://github.com/dmuth.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# Monte Carlo Simulation of Pi in Rust\n\nFor awhile now, I've wanted to get into Rust and play around with things like its multithreading\nfeatures.  So I figured a good way to do that would be to try and compute the value of Pi \nusing the Monte Carlo method as fast as possible.  I chose this method because it didn't require\nme to find hosting or use external data sources.\n\nIf you want to see how this code performs in benchmarking [look over here](benchmarks.md)!\n\n\n## How it works\n\n\u003cimg src=\"img/quadrant.png\" width=\"300\" align=\"right\" /\u003e\n\nThis app uses a simplification of the Monte Carlo method by just working with a \nquarter of a circle.  This allows me to keep all integers unsigned which makes the code\neasier to work with (and debug).  \n\nThis is done by representing the quadrant as a number of points on a cartesian plane plus 1.\nSo for example, if the number of points is 10  (the default), the possible x and y values range\nfrom 0 to 10 with (0,0) being the center of the circle and (0,10) and (10,0) being the corners\nof that square where the edges of the circle touch the edges of the square.\n\nFrom there, a number of random points (default 100) are generated, each point is evalated to see\nif it is in the circle (using the Pythagorean Theorem), and at the end the number of points \nin the circle are divided into the total number of points and multiplied by four.  This gives\nus an approximation of Pi.\n\n\n## CLI Args\n\nThe absolute most up to date args can be found by running `monte_carlo -h`, but here's \na brief introduction to command line options:\n\n- `-g, --grid-size` - How big to make each axis of the grid?  Bigger sizes will yield more precise numbers for Pi.\n- `-c, --count` - How many random points to generate in total?\n- `-b, --batch-size` - How many random points per loop in each thread?\n- `-n, --num-threads` - How many threads to use for random point generation?\n- `-m, --metrics` - Set if you want metrics printed out in JSON format.\n- `-t, --turbo` - Use \"turbo\" mode where a simplfied version of the Pythagorean Theorem is used.\n- `--cache` - Set to use caching for \"is this point inside the circle?\" calculations.\n- `--cache-precompute` - Precompute the entire cache.  Implies `--cache`.\n- `--benchmark` - Benchmark mode.  This will print how many points per second were plotted.\n- `-a, --avg-multiple-runs` - Perform multiple runs and average the values of Pi from all runs.\n- `-r, --random-seed` - ADVANCED: Seed the random number generator with a value for deterministic behavior.\n  - This is only effective when used in a single thread.  Multiple threads are NOT deterministic because each thread gets its own RNG \n\n\n## Architecture\n\n- App - Top level module/struct, that drives everything.  It spawns threads and performs calculations in threads.\n- Args - Processes our command-line arguments\n- Cache - Stores array of points and whether they are in the circle or not.  This prevents the same calculation from being run on multiple points.\n- Grid - Hold size of grid and number of points left to generate - Sits in main thread\n- Metrics - Used to hold metrics for our current run such as number of points, time elapsed, etc.\n- Point - represents a single point\n- Points - represents many points.  Pythagorean Theorem calculations happen here.\n- Random - Generates random numbers\n\n\n## Development\n\n- Test and run\n  - `cargo test -- --nocapture \u0026\u0026 cargo run --`\n- Test only\n  - `cargo test -- --nocapture`\n  - `--nocapture` lets me use `println!()` and similar to write to stdout\n- Build for prod\n  - `cargo build --release`\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmuth%2Fmonte-carlo-in-rust","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdmuth%2Fmonte-carlo-in-rust","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmuth%2Fmonte-carlo-in-rust/lists"}