Fast Tuple Shuffle (Fisher-Yates)

Fisher–Yates shuffle

shuffle(Tuple) ->
    shuffle(Tuple, size(Tuple)).

shuffle(Tuple, 1) ->
    Tuple;
shuffle(Tuple, N)->
    Random = erlang:phash2(os:timestamp(), N) + 1,
    A = element(N, Tuple),
    B = element(Random, Tuple),
    Tuple2 = setelement(N, Tuple, B),
    Tuple3 = setelement(Random, Tuple2, A),
    shuffle(Tuple3, N - 1).
 
12
Kudos
 
12
Kudos