(случайный выбор)
Bob Ippolito (@etrepum)
Erlang Factory Lite 2012, Moscow
○ (heads), X < 0.5
● (tails), X ≥ 0.5
choose(L) ->
Nth = random:uniform(length(L)),
{H, [Choice | T]} = lists:split(Nth, L),
{Choice, H ++ T}.
weight_split(N, L) -> weight_split(N, L, []).
weight_split(N, L, [H={K, W} | T], Acc) ->
case N - W of
N1 when N1 > 0 ->
weight_split(N1, T, [H | Acc]);
_ ->
{K, lists:reverse(Acc, T)}
end.
choose(L) ->
lists:nth(random:uniform(length(L)), L).
head | tail |
---|---|
a 1 | undefined |
b ⅔ | a ⅓ |
c ⅔ | a ⅓ |
d ⅔ | a ⅓ |
Key | Weight |
---|
/
#