蒙地卡羅法求 PI

蒙地卡羅法求 PI
 
說明

蒙地卡羅為摩洛哥王國之首都,該國位於法國與義大利國境,以賭博聞名。蒙地卡羅的基本原理為以亂數配合面積公式來進行解題,這種以機率來解題的方式帶有賭博的意味,雖然在精確度上有所疑慮,但其解題的思考方向卻是個值得學習的方式。

解法

蒙地卡羅的解法適用於與面積有關的題目,例如求PI值或橢圓面積,這邊介紹如何求PI值;假設有一個圓半徑為1,所以四分之一圓面積就為PI,而包括此四分之一圓的正方形面積就為1,如下圖所示:

蒙地卡羅


如果隨意的在正方形中投射飛標(點)好了,則這些飛標(點)有些會落於四分之一圓內,假設所投射的飛標(點)有n點,在圓內的飛標(點)有c點,則依比例來算,就會得到上圖中最後的公式。

至於如何判斷所產生的點落於圓內,很簡單,令亂數產生X與Y兩個數值,如果X^2+Y^2等於1就是落在圓內。

实现

  • C

    #include <stdio.h> 
    #include 
    <stdlib.h> 
    #include 
    <time.h> 
    #define N 50000 int 
    main(
    void{
         
    int i, sum = 0
        
    double x, y;
         srand(time(NULL));
         
    for(i = 1; i < N; i++{
             x 
    = (double) rand() / RAND_MAX;
             y 
    = (double) rand() / RAND_MAX;
             
    if((x * x + y * y) < 1)
                 sum
    ++;
         }

         printf(
    "PI = %f ", (double4 * sum / N);
         
    return 0;
     }
     


  • Java

    public class PI {
        
    public static void main(String[] args) {
            
    final int N = 50000
            
    int sum = 0;
            
    for(int i = 1; i < N; i++{
                 
    double x = Math.random();
                 
    double y = Math.random();
                 
    if((x * x + y * y) < 1
                    sum
    ++
            }

             System.out.println(
    "PI = " + (double4 * sum / N); 
         }

    }


  • VB.NET

        Private Sub calcurate()
            
    Dim x, y As Double
            
    Dim count As Integer = 0
            
    Dim round As Integer = 1000
            
    Dim p As Double
            
    For i As Integer = 0 To round
                x 
    = Rnd()
                y 
    = Rnd()
                
    If x * x + y * y <= 1 Then
                    count 
    += 1
                
    End If
            
    Next
            p 
    = count * 4 / round
            Debug.WriteLine(
    "PI = " + p.ToString)
        
    End Sub

<script type="text/javascript"> </script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>

name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-9750319131714390&dt=1176872394180&lmt=1175663639&format=160x600_as&output=html&url=http%3A%2F%2Fcaterpillar.onlyfun.net%2FGossip%2FAlgorithmGossip%2FMathPI.htm&ad_type=text_image&cc=524&u_h=1050&u_w=1400&u_ah=1020&u_aw=1400&u_cd=32&u_tz=540&u_java=true" frameborder="0" width="160" scrolling="no" height="600" allowtransparency="allowtransparency">

<script type="text/javascript"> </script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script> name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-9750319131714390&dt=1176872394212&lmt=1175663639&prev_fmts=160x600_as&format=160x600_as&output=html&url=http%3A%2F%2Fcaterpillar.onlyfun.net%2FGossip%2FAlgorithmGossip%2FMathPI.htm&ad_type=text_image&cc=524&u_h=1050&u_w=1400&u_ah=1020&u_aw=1400&u_cd=32&u_tz=540&u_java=true" frameborder="0" width="160" scrolling="no" height="600" allowtransparency="allowtransparency">


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值