# Shuffle

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

## Function Shuffle(a , i)

Shuffle returns a random reordering (permutation) of the values in array «a» over index «i». If you omit «i», by default it shuffles over Run -- i.e. it shuffles a random sample. You can use it generate an independent random sample from an existing probability distribution, «a».

## Independent Shuffles

If «a» has two dimensions, say J and Run, Shuffle(a, J) shuffles the sample over J independently for each value of Run. Shuffle(a, Run) -- or Shuffle(a), which is equivalent, because it assumes Run by default -- will shuffle each sample independently for each value of J.

Like other distribution functions, you can add an extra dimension using the «Over» parameter. For example, if B is a sample with no indexes other than Run,

Shuffle(B, Over: J)

returns an array of samples indexed by J, where the sample for each value of J is shuffled independently.

If you want to shuffle the slices of a multidimensional array over index «i», without shuffling the values within each slice, use this method:

A[@I = Shuffle(@I, I)]

This shuffles A over index I, but does not shuffle each slice of A for each value of I.

## Shuffling along the implicit index

You cannot shuffle along the implicit index, since the «i» parameter defaults to be the Run index when omitted. So if you want to shuffle along an implicit index, things get tricky.

If you need to shuffle a list, then make sure your list is held by a local variable so that it has a name. Once it has a name, you have a way to refer to it. So for example, this does not have the intended result

Shuffle(1..100)

but this does work because we have a way to name the implicit dimension

Var L:= 1..100 Do Shuffle(L, L)

If you have a multidimensional array, A, where one index is implicit and you want to shuffle along the implicit dimension, you can't obtain a name for the implicit index from A, so you have to introduce a name somehow. This requires creating a new implicit or explicit dimension that has a name, and reindexing the array onto this. This is done as follows.

Var I := 1..Size(A, ListLen: true) Do Shuffle(Slice(A, I), I)