Erlang 从列表中随机取出所有元素
Erlang 从列表中随机取出所有元素,使用random:uniform(N)来生成随机数。
randomOrder(List) -> Index = random(length(List)),%%io:format("Index: ~p, list: ~p", [Index,List]),Element = lists:nth(Index, List),io:format("get element:~p~n", [Element]),ListBefore = lists:sublist(List, Index-1),ListAfter = lists:sublist(List, Index+1, length(List) - length(ListBefore) - 1),NewList = lists:append(ListBefore, ListAfter),NewLength = length(NewList),if NewLength =:= 0 ->NewList;true -> randomOrder(NewList)end.random(N) -> %io:format("seed: ~p~n", [N]),if is_integer(N) -> R = random:uniform(N),%io:format("~p is generated!~n", [R]),R;true -> okend.本博客由广州办公家具有限公司赞助。