Macaulay2 » Documentation
Packages » Macaulay2Doc » combinatorics » shuffle
next | previous | forward | backward | up | index | toc

shuffle -- shuffle a list randomly

Description

i1 : shuffle toList (0 .. 12)

o1 = {5, 3, 6, 8, 1, 2, 0, 9, 11, 4, 7, 12, 10}

o1 : List

Mutable lists are shuffled in place.

i2 : x = new MutableList from 0..12; peek x

o3 = MutableList{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
i4 : shuffle x; peek x

o5 = MutableList{2, 3, 4, 8, 0, 1, 12, 6, 10, 7, 11, 5, 9}

Is n is given, then a shuffled random sublist of length n is returned. Contrast this with randomSubset, which preserves the order.

i6 : shuffle(toList(0..12), 4)

o6 = {0, 3, 9, 7}

o6 : List

Currently, random has this behavior when given a list. However, in the near future, this will change to the behavior of randomElement.

i7 : random toList(0..12)
 -- the behavior of random(List) will change soon; use shuffle(List) instead

o7 = {9, 1, 4, 2, 8, 5, 6, 7, 11, 10, 0, 12, 3}

o7 : List

See also

Ways to use shuffle:

  • shuffle(List)
  • shuffle(List,ZZ)
  • shuffle(MutableList)

For the programmer

The object shuffle is a method function.


The source of this document is in /build/reproducible-path/macaulay2-1.26.05+ds/M2/Macaulay2/packages/Macaulay2Doc/functions/shuffle-doc.m2:43:0.