What is shuffle() in Euphoria?

Overview

The elements of a sequence are arranged in a particular order. But these elements/members of a sequence can be rearranged and given new indexes, using the shuffle method.

shuffle() method

The shuffle() method is an in-built Euphoria method which is accessed from the sequence.e standard library. This method takes in a sequence and randomly rearranges its elements in no particular order.

The diagram below is a simple illustration of how this method works:

The shuffle() method

Syntax

The syntax of the shuffle() method is given below:

shuffle(my_seq)

Parameters

my_seq: This is the sequence whose elements will be shuffled or rearranged.

We should remember to add the sequence.e file to our program file, like so:

std/sequence.e

Code example

Here is a demonstration code, which shows us how this function can be used:

include std/sequence.e
printf(1,"%s \n",{shuffle("this wordShuffling will give insight us a clearer")})
sequence seq1 = {1,2,3}, seq2 = {4,5,6}, output
output = shuffle({seq1,seq2})
print(1,{output})

Code explanation

  • Line 1: We include the sequence.e file.

  • Line 3: We print the output of the suffle() method operation to the console.

  • Line 5: We declare and assign values to the variables seq1, seq2, and seq3.

  • Line 7: We shuffle the members of the sequence, that is, variables seq1 and seq2 and save the output in the variable output.

  • Line 9: We display the content of the output.

Note: In the second output, the members of the sub-sequences that were passed to the shuffle() function were shuffled individually and so were the sub-sequences themselves.