| VB.NET | |
|
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 |
Console.Write("サンプル数? ") Dim s As Integer = Console.Read() Console.WriteLine("π = {0}", System.Math.PI) Dim i As Integer For i = 1 To 10 Dim n As Integer Dim m As Integer Dim rand As New System.Random(DateTime.Now.Millisecond * i) Dim x, y As Double n = 0 m = 0 Do x = rand.NextDouble() y = rand.NextDouble() If System.Math.Sqrt(x * x + y * y) < 1.0 Then m += 1 n += 1 Loop While n < s Console.WriteLine("{0}回目: π ≒ {1}", i.ToString("D2"), 4.0 * m / n) Next |
| 出力例 | |
サンプル数? 10000000000 π = 3.14159265358979 01回目: π ≒ 3.3469387755102 02回目: π ≒ 3.26530612244898 03回目: π ≒ 3.59183673469388 04回目: π ≒ 3.26530612244898 05回目: π ≒ 3.02040816326531 06回目: π ≒ 3.02040816326531 07回目: π ≒ 3.10204081632653 08回目: π ≒ 3.18367346938776 09回目: π ≒ 3.10204081632653 10回目: π ≒ 2.93877551020408 Press any key to continue | |