Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Challenge: Estimate Pi Using Monte Carlo Simulation | Probability, Statistics, and Simulation
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python for Mathematics

bookChallenge: Estimate Pi Using Monte Carlo Simulation

The Monte Carlo method is a powerful technique that uses randomness to solve problems that might be deterministic in principle. In mathematics and statistics, it is often used to estimate values that are difficult to compute directly. One classic application is estimating the value of pi (π).

To estimate pi using the Monte Carlo method, imagine a square with a circle perfectly inscribed within it. If you randomly generate points within the square, the proportion of points that fall inside the circle compared to the total number of points in the square will approximate the ratio of the areas of the circle and the square. Since the area of a circle with radius 1 is π, and the area of the square is 4 (since its side is 2), the ratio is π/4. Therefore, by multiplying the fraction of points inside the circle by 4, you can estimate the value of pi.

This approach demonstrates how randomness and probability can be harnessed to approximate mathematical constants, making Monte Carlo simulations a valuable tool in both theoretical and applied mathematics.

123456789101112
import random # Generate a random point (x, y) inside the square [-1, 1] x [-1, 1] x = random.uniform(-1, 1) y = random.uniform(-1, 1) # Check if the point falls inside the unit circle (centered at (0,0) with radius 1) distance_squared = x**2 + y**2 is_inside_circle = distance_squared <= 1 print("Random point:", (x, y)) print("Is inside circle:", is_inside_circle)
copy
Tâche

Swipe to start coding

Write a function that estimates the value of pi using the Monte Carlo method. The function should take the number of random points to generate as its argument, and use this many random (x, y) points inside the square from -1 to 1 on both axes.

  • Generate num_points random points with coordinates between -1 and 1 for both x and y.
  • Count how many points fall inside the unit circle (distance from the origin less than or equal to 1).
  • Calculate the estimate of pi as 4 times the ratio of points inside the circle to the total number of points.
  • Return the estimated value of pi.

Solution

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 3
single

single

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

close

bookChallenge: Estimate Pi Using Monte Carlo Simulation

Glissez pour afficher le menu

The Monte Carlo method is a powerful technique that uses randomness to solve problems that might be deterministic in principle. In mathematics and statistics, it is often used to estimate values that are difficult to compute directly. One classic application is estimating the value of pi (π).

To estimate pi using the Monte Carlo method, imagine a square with a circle perfectly inscribed within it. If you randomly generate points within the square, the proportion of points that fall inside the circle compared to the total number of points in the square will approximate the ratio of the areas of the circle and the square. Since the area of a circle with radius 1 is π, and the area of the square is 4 (since its side is 2), the ratio is π/4. Therefore, by multiplying the fraction of points inside the circle by 4, you can estimate the value of pi.

This approach demonstrates how randomness and probability can be harnessed to approximate mathematical constants, making Monte Carlo simulations a valuable tool in both theoretical and applied mathematics.

123456789101112
import random # Generate a random point (x, y) inside the square [-1, 1] x [-1, 1] x = random.uniform(-1, 1) y = random.uniform(-1, 1) # Check if the point falls inside the unit circle (centered at (0,0) with radius 1) distance_squared = x**2 + y**2 is_inside_circle = distance_squared <= 1 print("Random point:", (x, y)) print("Is inside circle:", is_inside_circle)
copy
Tâche

Swipe to start coding

Write a function that estimates the value of pi using the Monte Carlo method. The function should take the number of random points to generate as its argument, and use this many random (x, y) points inside the square from -1 to 1 on both axes.

  • Generate num_points random points with coordinates between -1 and 1 for both x and y.
  • Count how many points fall inside the unit circle (distance from the origin less than or equal to 1).
  • Calculate the estimate of pi as 4 times the ratio of points inside the circle to the total number of points.
  • Return the estimated value of pi.

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 3
single

single

some-alt