首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Erlang 从列表中随机掏出所有元素

2012-09-19 
Erlang 从列表中随机取出所有元素Erlang 从列表中随机取出所有元素,使用random:uniform(N)来生成随机数。ra

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.

本博客由广州办公家具有限公司赞助。

热点排行