erlang隨機排列數組

參考karl's answerdom

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.this

2>[X||{_, X} <- lists:sort([{random:uniform(), N} || N <- L])].
[5,2,3,1,7,4,6,9,10,8]
3>
相關文章
相關標籤/搜索