5 kyu

MONTE CARLO 3D

86 of 118raulbc777

Description:

In this kata we will see a method, not so accurate but a different one to estimate number Pi. You will be given a list of random points contained in a cube of side of length 2l. The center of the cube coincides with the center of the coordinates system xyz, the point (0, 0, 0). You can see the above image to understand that the radius of the spehere is half of the side of the cube. To estimate Pi we should count the amount of points that are contained in the sphere inscribed in the cube.

The function mCarlo3D_pi(), will receive a list of numeruous points. An estimation of the length side of the cube, and of course, the radius of the sphere, has to be deduced from this list.

The function mCarlo3D_pi() , will output in a list

[(1), (2), (3), (4), (5), (6)]

(1) - Total amount of points.

(2) - Estimated radius of the inscribed sphere.

(3) - Number of points contained in the sphere(including the ones in its surphace).

(4) - Estimation of Pi as a float with a maximum of four decimals (rounded result).

(5) - Relative error, relError= abs(real Val of Pi - estmimated Val of Pi) / (real Val of Pi) * 100 rounded with two decimals and expressed as a string with the symbol "%"

(6) - A boolean variable according to the constraints bellow:

     if relError  5 % -----> False
     if relError < 5 % ------> True

Let's see some cases:

list1 = [(-5, 3, -5), (3, 5, -6), (-5, 6, 0), (-5, 3, -6), (4, -3, -1), (5, 4, -3), (6, 4, -1), (5, 3, 2), (1, -2, 5), (-5, 0, -2)] (10 points)

mCarlo3D_pi(list1) -----> [10, 6, 3, 1.8, '42.7%', False] )

/// What? An estimation of pi equals to 1.8? mmm.... Let's try with a list with more points. We're too far from its real value. A relative error of 42.7% is too high!///


list2 = [(1, 4, 6), (0, 4, 5), (1, 3, -5), (1, 5, -2), (0, 5, 6), (-5, 2, 5), 
(6, 2, 6), (5, 2, 2), (0, 1, 5), (2, 0, 3), (2, 4, 0), (-3, -6, 3), (-6, 6, -6), 
(-5, -3, -1), (1, -5, 2), (4, 5, -4), (3, 1, -2), (-4, 0, 3), (3, -6, 5), (5, 4, -4),
 (5, 5, -6), (2, -3, 3), (5, 2, -4), (-2, 3, -3), (2, -4, 6), (-6, -5, 4), (1, -2, 0),
 (-4, -4, 3), (1, -1, -4), (-2, -5, -2)] (30 points)

mCarlo3D_pi(list2) -----> [30, 6, 15, 3.0, '4.51%', True]

/// Well, a bit better. This result gives us hope to try with larger lists and higher sizes for the cubes.

Before coding, you have to relate the quotient (points in sphere) / (total points in the cube) to the number pi.

(Hint: Monte Carlo2D -------> Monte Carlo3D.)

Enjoy it!! (Relative Error should be evaluated (≥ 5 or < 5) with all the decimals that Python gives to floats. It should be rounded only for the result as a string)

Fundamentals
Mathematics
Data Structures

Stats:

CreatedSep 16, 2015
PublishedSep 17, 2015
Warriors Trained471
Total Skips163
Total Code Submissions1630
Total Times Completed118
Python Completions86
Ruby Completions21
JavaScript Completions23
Total Stars10
% of votes with a positive feedback rating87% of 39
Total "Very Satisfied" Votes29
Total "Somewhat Satisfied" Votes10
Total "Not Satisfied" Votes0
Total Rank Assessments7
Average Assessed Rank
5 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • raulbc777 Avatar
  • Voile Avatar
Ad