-
Notifications
You must be signed in to change notification settings - Fork 0
/
casino.erl
38 lines (28 loc) · 855 Bytes
/
casino.erl
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
-module(casino).
-export([start_all/2, start_aggregator/0, aggregator/1, spin_machine/1]).
start_all(0, _) ->
io:format("All machines are spawned~n");
start_all(N, Aggregator) ->
spawn(casino, spin_machine, [Aggregator]),
start_all(N - 1, Aggregator).
spin_machine(Aggregator) ->
Aggregator ! {done, slot_machine:auto_spin(1000000)}.
start_aggregator() ->
register(aggregator, spawn(casino, aggregator, [[]])).
aggregator(Acc) ->
receive
{done, Paid} ->
aggregator([Paid | Acc]);
{say_payout} ->
io:format("Machines played: ~b Avg payout: ~f~n", [length(Acc), payout(Acc)]),
aggregator(Acc);
{flush} ->
aggregator([]);
_ ->
io:format("Message received~n")
end.
payout([]) ->
0.0;
payout(L) ->
Sum = lists:foldl(fun(X, Sum) -> X + Sum end, 0, L),
Sum * 1.0 / length(L) / 1000000.