Shuffle

From Xojo Documentation

(Redirected from Arrays.Shuffle)
You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!
Method

Arrays.Shuffle()

Supported for all project types and targets.

Shuffles (rearranges randomly) the elements of an array.

Usage

array.Shuffle

Part Type Description
array Any valid data type The array whose elements will be shuffled.

Notes

Shuffle works on arrays of any data type and any number of dimensions. It is based on a random number. An element of the original array has a roughly equal chance of appearing in any cell of the array after the shuffle, regardless of the size and number of dimensions of the array.

A Fisher-Yates shuffle is used. There is no ability to control the seed.

Sample Code

Shuffle a one-dimensional array:

Var values() As Integer = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
values.Shuffle

Var result As String
For i As Integer = 0 To values.LastRowIndex
result = result + i.ToString
Next

Shuffle a two-dimensional array:

Var myArray(5, 5) As Text
For i As Integer = 0 To 5
For j As Integer = 0 To 5
myArray(i, j) = i.ToString + j.ToString
Next
Next
myArray.Shuffle

See Also

Var statement; Arrays concept for a complete list of functions; ParamArray keyword