# How can I randomize order of elements in a list?

Is there a straightforward way to go from an ordered list like:

ord = {a, b, c, d, e, f, g, h}


to a disordered list like:

randompos = {c, h, d, g, a, b, f, e}


I can certainly use something like:

DeleteDuplicates @ RandomChoice[ord, 50]


but this is not really robust.

Is there an easier way?

• Use RandomSample. Jun 25, 2013 at 3:44
• @Spawn1701D RandomSample won't be able to create the letter i out of thin air. The OP needs a god Jun 25, 2013 at 3:46
• @Spawn1701D That is a simplistic way :) Jun 25, 2013 at 3:48
• @belisarius typo fixed. i have no need for the divine. Jun 25, 2013 at 4:09
• RandomSample[#,Length[#]]&@ord Jun 25, 2013 at 4:13

# Method1 RandomSample

as @Spawn1701D and @Oleksandr R.said in comment,

RandomSample[ord] is sufficient.

ord = {a, b, c, d, e, f, g, h};

RandomSample[ord]

(*
{c,e,g,b,h,f,a,d}
*)


To get one sublist

RandomSample[#, Length[#] - 4] & @ ord

(*
{g,h,e,f}
*)


# Method2 RandomPermutation

SeedRandom[1234]; Permute[ord, RandomPermutation[8]]

(*
{b,f,e,h,d,g,c,a}
*)


One aside: sometimes when people's comments are apparently feasible to the question, they might not likely to answer the post, others may wish the first people who leave the good comment to answer the question, and OP is also encouraged to answer its own question.