{"id":26578837,"url":"https://github.com/pprunty/monte_carlo_pi","last_synced_at":"2025-03-23T05:17:15.059Z","repository":{"id":168300395,"uuid":"454145192","full_name":"pprunty/monte_carlo_pi","owner":"pprunty","description":null,"archived":false,"fork":false,"pushed_at":"2023-11-23T23:43:50.000Z","size":3209,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2023-11-24T00:28:03.217Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","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/pprunty.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}},"created_at":"2022-01-31T19:41:05.000Z","updated_at":"2022-01-31T19:41:26.000Z","dependencies_parsed_at":"2023-11-24T00:27:24.059Z","dependency_job_id":"ad7bc175-f302-49f4-9cf6-b5ca13441354","html_url":"https://github.com/pprunty/monte_carlo_pi","commit_stats":null,"previous_names":["pprunty/monte_carlo_pi"],"tags_count":0,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pprunty%2Fmonte_carlo_pi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pprunty%2Fmonte_carlo_pi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pprunty%2Fmonte_carlo_pi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pprunty%2Fmonte_carlo_pi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pprunty","download_url":"https://codeload.github.com/pprunty/monte_carlo_pi/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245056898,"owners_count":20553856,"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-03-23T05:17:14.469Z","updated_at":"2025-03-23T05:17:15.028Z","avatar_url":"https://github.com/pprunty.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"Part of a college assignment...\n\n# Monte Carlo π\n\nThis is a simple program used to approximate Pi using a Monte Carlo method. This is an exercise\noften used to introduce students to Monte Carlo methods.\n\n## Contents\n\n1. [Overview](#overview)\n2. [Requirements](#requirements)\n3. [Compilation and Usage](#compilation-and-usage)\n4. [Known Issues](#known-issues)\n\n![Screenshot](graphics/N_1000.png)\n\n## Overview\n\nConsider a quadrant (circular sector) inscribed in a unit square. Given that the ratio of their areas is\nπ / 4 , the value of π can be approximated using a Monte Carlo method:\n\n1. Draw a square, then inscribe a quadrant within it\n2. Uniformly scatter a given number of points over the square\n3. Count the number of points inside the quadrant, i.e. having a distance from the origin of less than 1\n4. The ratio of the inside-count and the total-sample-count is an estimate of the ratio of the two areas,\n   π / 4. Multiply the result by 4 to estimate π.\n\nIn this procedure the domain of inputs is the square that circumscribes the quadrant. We generate random \ninputs by scattering grains over the square then perform a computation on each input (test whether it falls within the \nquadrant). Aggregating the results yields our final result, the approximation of π.\n\nThere are two important considerations:\n\n1. If the points are not uniformly distributed, then the approximation will be poor\n2. There are many points. The approximation is generally poor if only a few points are randomly placed in the whole\n   square. On average, the approximation improves as more points are placed.\n\nUses of Monte Carlo methods require large amounts of random numbers, and it was their use that spurred the development \nof pseudorandom number generators, which were far quicker to use than the tables of random numbers that had been previously used for statistical sampling.\n\n## Requirements\n\n- gcc\n- gnuplot (if you would like to produce graphics)\n- gsl (for using Sobol qrng, otherwise comment out that part of the code (L.48-56 in coord.cc))\n\n## Compilation and Usage\n\nTo compile the code simply run the following command inside a terminal in the project directory:\n\n```shell\nmake\n```\n\nTo produce graphics, run the following commands inside the gnuplot terminal int he project directory:\n\n```shell\nset parametric\nset size square\nset xrange [0:1]\nset yrange [0:1]\nunset key\nplot [0:2*pi] 1*sin(t), 1*cos(t), 'misses.txt' title \"Misses\" with points pointtype 7, \"hits.txt\" title \"Hits\" with points pointtype 7\n```\n\nNote: I know I can add this to the Makefile as 'make plot' but I prefer to leave the commands here so I can return to them.\n\n## Results\n\nAs expected, our approximation of π gets better as the number of simulations N increases.\n\nN = 100\n\n![Screenshot](graphics/N_100.png)\n\nOutput:\n\n```shell\nEnter the number of simulations N to approximate π = 10000\nWould you like to use Sobol's quasi-random number generator [y/n]?  n\nWriting results to file: points.txt\nWriting results to file: hits.txt\nWriting results to file: misses.txt\nApproximation of π = 3.12 for 100 simulations.\n```\n\nN = 1,000\n\n![Screenshot](graphics/N_1000.png)\n\nOutput:\n\n```shell\nEnter the number of simulations N to approximate π = 10000\nWould you like to use Sobol's quasi-random number generator [y/n]?  n\nWriting results to file: points.txt\nWriting results to file: hits.txt\nWriting results to file: misses.txt\nApproximation of π = 3.104 for 100 simulations.\n```\n\nN = 10,000\n\n![Screenshot](graphics/N_10000.png)\n\nOutput:\n\n```shell\nEnter the number of simulations N to approximate π = 10000\nWould you like to use Sobol's quasi-random number generator [y/n]?  n\nWriting results to file: points.txt\nWriting results to file: hits.txt\nWriting results to file: misses.txt\nApproximation of π = 3.1392 for 100 simulations.\n```\n\nAs an aside, we also get quicker convergence when we use what is known as a quasi-random number generator such as\nSobol. Here is an example for N = 1,000 simulations using Sobol:\n\n\n![Screenshot](graphics/N_1000_sobol.png)\n\nOutput: \n\n```shell\nEnter the number of simulations N to approximate π = 1000\nWould you like to use Sobol's quasi-random number generator [y/n]?  y\nWriting results to file: points.txt\nWriting results to file: hits.txt\nWriting results to file: misses.txt\nApproximation of π = 3.144 for 1000 simulations.\n```\n\nTo review Sobol and quasi-random number generators, check out this link [here](https://en.wikipedia.org/wiki/Sobol_sequence)\n\n\n\n## Known Issues\n\nNone at present.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpprunty%2Fmonte_carlo_pi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpprunty%2Fmonte_carlo_pi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpprunty%2Fmonte_carlo_pi/lists"}