![]() ![]() This page was generated using Literate.jl. Plotting Pi using Monte Carlo Method Ask Question Asked 6 years, 2 months ago Modified 4 years, 5 months ago Viewed 12k times 2 I can evaluate the value of pi using different data points by Python. This lets us use "independent" RNG for each ctr-th iteration: function monte_carlo_pi(n, m = 10_000, ex = has_cuda_gpu() ? CUDAEx() : ex for ctr in counters(n)Įnd πₐₚₚᵣₒₓ = monte_carlo_pi(2^12) 3.1411634765625 Return UInt64(0):stride:typemax(UInt64)-stride We then generate a large number of random points within the square and count how many fall in the enclosed circle. This methods consists of drawing on a canvas a square with an inner circle. Let's create a helper function that divides UInt64(0):typemax(UInt64) into n equal intervals: function counters(n) One method to estimate the value of (3.141592.) is by using a Monte Carlo method. Rand(rng_b, UInt64, rng_a = rng_b Using counter-based RNG ![]() This RNG gives us two UInt64s for each counter which wraps around at typemax(UInt64): rng_a = Philox2x(0) The idea of the calculation of PI using the Monte Carlo method is to sample random points in a square, and count the fraction of them that fall within the area of a circle bound by that square. In this example, we use Random123.Philox2x. In particular, we use a Counter-based random number generator (CBRNG) provided by Random123.jl ( documentation). However, we can use pure-Julia pseudo number generator quite easily. The Monte Carlo method was invented by scientists working on the atomic bomb in the 1940s, who named it for the city in Monaco famed for its casinos and games of chance. We try to do this computation on a GPU using FoldsCUDA.jl: using CUDAĪs of writing, CUDA.CURAND does not provide the API usable inside the loop body (i.e., the device API). –- File:Pi 30K.gif - Wikipedia by nicoguaro is licensed under CC BY 3.0. Since the probably $p$ for a point to be inside the quadrant is the area of the quadrant ( $= π / 4$) divided by the area of the unit square ( $= 1$), we can estimate $π$ by estimating the probability $p = π / 4$: xs = rand(10_000) ![]() The idea is to draw points from the uniform distribution on a unit square and count the ratio of points that are inside the quadrant of the unit circle. In this technique, we start finding a model. Let's compute an approximation of π using the Monte Carlo method. The Monte Carlo method provides an approximate result due to probabilities used in the related theoretical calculus. Estimating π using Monte-Carlo method Idea (points in the circle)/(total points) (piradius2)/(2radius)2. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |