Skip to content

ShuffleArray

See Also

Purpose

Returns an array with elements in a shuffled (random) order from the original array.

Return Type

Array

Syntax

(ShuffleArray({ArrayId}))

Parameters

  • {ArrayId}: The ID of the array being shuffled. The array must be non-jagged and one-dimensional (it can be a single dimension of a multi-dimensional array).

What it Does

ShuffleArray changes the order of the elements in an array to a random order.

Examples

Example 1: Shuffling a Dynamic Array of Integers

This sample shuffles a dynamic array of integers (ints1) and places the result into the array ints2.

// fires when the button is clicked
Procedure OnClick
    Integer[2][3] ints1
    Integer[2][3] ints2
    // add some code to populate ints1 array
    // :
    // shuffle the array
    Move (ShuffleArray(ints1)) to ints2
End_Procedure

Example 2: Shuffling an Array of Strings

This sample shuffles an array of 10 strings (sCustomers), then displays the shuffled array elements to the screen.

// fires when the button is clicked
Procedure OnClick
    String[] sCustomers
    Integer i iArraySize
    // populate array
    Move "Smith" to sCustomers[0]
    Move "Rodriguez" to sCustomers[1]
    Move "Smith" to sCustomers[2]
    Move "Jones" to sCustomers[3]
    Move "Anderson" to sCustomers[4]
    Move "Schmidt" to sCustomers[5]
    Move "Verne" to sCustomers[6]
    Move "Ricci" to sCustomers[7]
    Move "Sorensen" to sCustomers[8]
    Move "Garcia" to sCustomers[9]
    // randomize the array
    Move (ShuffleArray(sCustomers)) to sCustomers
    // display the elements in the shuffled array
    Move (SizeOfArray(sCustomers)) to iArraySize
    For i From 0 to (iArraySize - 1)
        Showln sCustomers[i]
    Loop
End_Procedure

Example 3: Shuffling a Row of a Multi-Dimensional Array

This sample declares a 2-dimensional string array with 8 (2x4) elements and fills it with unsorted string values. This creates an array as follows:

Smith      Rodriguez   Scott      Jones
Anderson   Schmidt     Verne      Ricci

The first "row" (row 0) of the array is then shuffled and placed back in row 0 of the resulting array. So, the first "row" of the array is randomized.

Procedure OnClick
    String[2][4] sCustomers
    Integer i j
    // populate array
    Move "Smith"     to sCustomers[0][0]
    Move "Rodriguez" to sCustomers[0][1]
    Move "Scott"     to sCustomers[0][2]
    Move "Jones"     to sCustomers[0][3]
    Move "Anderson"  to sCustomers[1][0]
    Move "Schmidt"   to sCustomers[1][1]
    Move "Verne"     to sCustomers[1][2]
    Move "Ricci"     to sCustomers[1][3]
    // randomize the array's first "row"
    Move (ShuffleArray(sCustomers[0])) to sCustomers[0]
End_Procedure