-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabb_carlo.ex
93 lines (76 loc) · 2.55 KB
/
labb_carlo.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
defmodule Carlo do
def dart(radius) do
x = Enum.random(0..radius)
y = Enum.random(0..radius)
:math.pow(x, 2) + :math.pow(y, 2) < :math.pow(radius, 2)
end
def round(0, _, a) do a end
def round(k, r, a) do
if(dart(r)) do
round(k-1, r, a+1)
else
round(k-1, r, a)
end
end
def rounds(k, j, r) do
rounds(k, j, 0, r, 0)
end
def rounds(0, _, t, _, a) do a/t * 4 end # beräkna medel?
# Variabel betydelser
# k -> antalet rounds att göra
# j -> antalet test per round
# t -> totalt gjorda test
# r -> radius
# a -> ackumulator
def rounds(k, j, t, r, a) do
a = round(j, r, a)
t = t+j
pi = a/t * 4.0 #?
#:io.format("~w, ~.7f, ~.7f\n", [t, pi, (pi - :math.pi())])
:io.format("~w, ~.7f, ~.7f, ~.7f, ~.7f, ~.7f, ~.7f, ~.7f\n", [t, Kernel.abs(pi - :math.pi()), 0.05, 0.005, 0.0005, 0.00005 , 0.000005, 0.0000005])
rounds(k-1, j, t, r, a)
end
def rounds_double(k, j, r) do
rounds_double(k, j, 0, r, 0)
end
def rounds_double(0, _, t, _, a) do a/t * 4 end # beräkna medel?
# Variabel betydelser
# k -> antalet rounds att göra
# j -> antalet test per round
# t -> totalt gjorda test
# r -> radius
# a -> ackumulator
def rounds_double(k, j, t, r, a) do
a = round(j, r, a)
t = t+j
pi = a/t * 4.0 #?
#:io.format("~w, ~.7f, ~.7f\n", [t, pi, (pi - :math.pi())])
:io.format("~w, ~.7f, ~.7f, ~.7f, ~.7f, ~.7f, ~.7f, ~.7f\n", [t, Kernel.abs(pi - :math.pi()), 0.05, 0.005, 0.0005, 0.00005 , 0.000005, 0.0000005])
rounds_double(k-1, j*2, t, r, a)
end
def createTasks(tasks, _, 0) do tasks end
def createTasks(tasks, rounds, num_to_create) do
task = Task.async(Carlo, :rounds, [1, rounds, 1000000000])
createTasks([task|tasks], rounds, num_to_create-1)
end
def multiThread(threads, rounds) do
tasks = createTasks([], rounds, threads)
sum = Enum.reduce(tasks, 0,
fn(task, acc) ->
acc + Task.await(task, 1000*3600*24) # one day timeout
end
)
sum / threads
end
def doHeavyTest(rounds_per_thread, darts_per_round, threads) do
test_list = for i <- 1..rounds_per_thread do i end
res = Enum.map(test_list, fn(x) ->
IO.puts("#{x/rounds_per_thread*100.0}% done")
multiThread(threads, darts_per_round)
end)
#res
appr = (res |> Enum.sum())/rounds_per_thread
IO.puts("DONE")
:io.format("~w, ~.7f, ~.7f, ~.7f, ~.7f, ~.7f, ~.7f, ~.7f\n", [threads*rounds_per_thread*darts_per_round, Kernel.abs(appr - :math.pi()), 0.05, 0.005, 0.0005, 0.00005 , 0.000005, 0.0000005])
end
end