June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
34
Task/Monte-Carlo-methods/Go/monte-carlo-methods-2.go
Normal file
34
Task/Monte-Carlo-methods/Go/monte-carlo-methods-2.go
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"golang.org/x/exp/rand"
|
||||
)
|
||||
|
||||
func getPi(numThrows int) float64 {
|
||||
inCircle := 0
|
||||
for i := 0; i < numThrows; i++ {
|
||||
//a square with a side of length 2 centered at 0 has
|
||||
//x and y range of -1 to 1
|
||||
randX := rand.Float64()*2 - 1 //range -1 to 1
|
||||
randY := rand.Float64()*2 - 1 //range -1 to 1
|
||||
//distance from (0,0) = sqrt((x-0)^2+(y-0)^2)
|
||||
dist := math.Hypot(randX, randY)
|
||||
if dist < 1 { //circle with diameter of 2 has radius of 1
|
||||
inCircle++
|
||||
}
|
||||
}
|
||||
return 4 * float64(inCircle) / float64(numThrows)
|
||||
}
|
||||
|
||||
func main() {
|
||||
rand.Seed(uint64(time.Now().UnixNano()))
|
||||
fmt.Println(getPi(10000))
|
||||
fmt.Println(getPi(100000))
|
||||
fmt.Println(getPi(1000000))
|
||||
fmt.Println(getPi(10000000))
|
||||
fmt.Println(getPi(100000000))
|
||||
}
|
||||
|
|
@ -1,7 +1,9 @@
|
|||
function montepi(n)
|
||||
s = 0
|
||||
for i = 1:n
|
||||
s += rand()^2 + rand()^2 < 1
|
||||
end
|
||||
return 4*s/n
|
||||
function monteπ(n)
|
||||
s = count(rand() ^ 2 + rand() ^ 2 < 1 for _ in 1:n)
|
||||
return 4s / n
|
||||
end
|
||||
|
||||
for n in 10 .^ (3:8)
|
||||
p = monteπ(n)
|
||||
println("$(lpad(n, 9)): π ≈ $(lpad(p, 10)), pct.err = ", @sprintf("%2.5f%%", abs(p - π) / π))
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,9 +1,8 @@
|
|||
def approx_pi(throws)
|
||||
times_inside = throws.times.count {Math.hypot(rand, rand) <= 1.0}
|
||||
# in pre-Ruby-1.8.7: times_inside = throws.times.select {Math.hypot(rand, rand) <= 1.0}.length
|
||||
4.0 * times_inside / throws
|
||||
end
|
||||
|
||||
[1000, 10_000, 100_000, 1_000_000, 10_000_000].each do |n|
|
||||
puts "%8d samples: PI = %s" % [n, approx_pi(n)]
|
||||
puts "%8d samples: PI = %s" % [n, approx_pi(n)]
|
||||
end
|
||||
|
|
|
|||
33
Task/Monte-Carlo-methods/Rust/monte-carlo-methods.rust
Normal file
33
Task/Monte-Carlo-methods/Rust/monte-carlo-methods.rust
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
extern crate rand;
|
||||
|
||||
use rand::Rng;
|
||||
use std::f64::consts::PI;
|
||||
|
||||
// `(f32, f32)` would be faster for some RNGs (including `rand::thread_rng` on 32-bit platforms
|
||||
// and `rand::weak_rng` as of rand v0.4) as `next_u64` combines two `next_u32`s if not natively
|
||||
// supported by the RNG. It would less accurate however.
|
||||
fn is_inside_circle((x, y): (f64, f64)) -> bool {
|
||||
x * x + y * y <= 1.0
|
||||
}
|
||||
|
||||
fn simulate<R: Rng>(rng: &mut R, samples: usize) -> f64 {
|
||||
let mut count = 0;
|
||||
for _ in 0..samples {
|
||||
if is_inside_circle(rng.gen()) {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
(count as f64) / (samples as f64)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut rng = rand::weak_rng();
|
||||
|
||||
println!("Real pi: {}", PI);
|
||||
|
||||
for samples in (3..9).map(|e| 10_usize.pow(e)) {
|
||||
let estimate = 4.0 * simulate(&mut rng, samples);
|
||||
let deviation = 100.0 * (1.0 - estimate / PI).abs();
|
||||
println!("{:9}: {:<11} dev: {:.5}%", samples, estimate, deviation);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue