p5.js shuffle() function
Last Updated :
22 Aug, 2023
The shuffle() function in p5.js is used to shuffle the order of given array elements.
Syntax:
shuffle(Array)
Parameters: This function accepts a parameter Array whose elements are to be shuffled.
Return Value: It returns the shuffled array.
Below program illustrates the shuffle() function in p5.js:
Example-1:
function setup() {
createCanvas(500, 90);
background(220);
let Array1 = [ 'IT' , 'CSE' , 'ECE' ];
let Array2 = shuffle(Array1);
textSize(16);
fill(color( 'red' ));
text( "Shuffled array is : " + Array2, 50, 30);
}
|
Output:
Example-2:
function setup() {
createCanvas(500, 90);
background(220);
let Array = shuffle([ 'Ram' , 'Shayam' , 'Geeta' , 'Anita' ]);
textSize(16);
fill(color( 'red' ));
text( "Shuffled array is : " + Array, 50, 30);
}
|
Output:
NOTE: In the above codes, draw() function has not been used because if we use draw function then it does not give a clear output i.e, it keeps on changing the order of the array string continually and not giving any stagnant result.
Reference: https://p5js.org/reference/#/p5/shuffle
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...