Detailed example of Java Monte Carlo algorithm for approximate value of PI

origin

[1946: John von Neumann,Stan Ulam,and Nick Metropolis,all at the Los Alamos Scientific Laboratory,cook up the Metropolis algorithm,also kNown as the Monte Carlo method.] In 1946, John von Neumann, Stan Ulam and Nick metropolis, three scientists from the Las Amos National Laboratory of the United States, jointly invented the Monte Carlo method. Its specific definition is: draw a square with a side length of one meter on the square, and draw an irregular shape with chalk inside the square. Now to calculate the area of the irregular shape, how to calculate the column? The Monte Carlo method tells us, Evenly sprinkle n (n is a large natural number) soybeans into the square, and then count how many soybeans are in the irregular geometry, for example, m, then the area of the strange shape is similar to m / N. the larger n is, the more accurate the calculated value is. Here, we should assume that the beans are all on a plane and do not overlap each other. (spreading soybeans is just a metaphor.)

characteristic

The greatness of Monte Carlo method is to use the idea of "simulation" to solve the problem of accuracy when it can not be solved. It has been applied in various fields. The essence is simulation: a large number of random inputs are used to generate various outputs; the probability distribution of the result is the "approximation" of the real distribution. Therefore, whether the distribution of the input is random (what the computer can do at present is pseudo-random, and can not produce a real random distribution), this process is called sampling random variables.

Code for calculating the approximate value of PI:

Output:

summary

The above is a brief introduction to the origin and characteristics of Monte Carlo algorithm, as well as an example of how to use this algorithm to find the approximate value of PI in Java programming. I hope it will be helpful to you. Like friends, please continue to pay attention to programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>