erlang随机排列数组

参考karl's answer

1> L = lists:seq(1,10).
[1,2,3,4,5,6,7,8,9,10]

Associate a random number R with each element X in L by making a list of tuples {R, X}. Sort this list and unpack the tuples to get a shuffled version of L.

2>[X||{_, X} <- lists:sort([{random:uniform(), N} || N <- L])].
[5,2,3,1,7,4,6,9,10,8]
3>

 

posted @ 2015-10-22 16:43  格通  阅读(253)  评论(0编辑  收藏  举报