We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey, just wondering if anyone could give me a hand with shuffling.
I've got an char array and I would like to shuffle and print the updated array when the mouse is pressed.
Thanks :)
char [] letters;
void setup() {
size (500, 500);
creating_letters();
}
void draw () {
background(200);
//input size here
printing_letters(30);
}
void creating_letters () {
letters = new char [20];
for (int i = 0; i < letters.length; i++) {
letters [i] = (char) int(random(33, 100));
}
}
void printing_letters(int size) {
int textSize = size;
for (int i = 0; i<letters.length; i++) {
textSize(textSize);
fill(0);
text((letters[i]), ((i*(textSize/1.3))+10), height/2);
fill(255);
}
if ((mousePressed) && (mouseButton == LEFT)){
Shuffle();
}
}
void Shuffle () {
//not sure what to do :/
letters = sort (letters, int(random(letters.length)));
}
Answers
https://forum.Processing.org/two/discussion/20529/retrieving-elements-from-a-list-randomly#Item_5
Sorry i'm kinda new to programming >__<
so, can I add ".toCharArray()" onto the end of the statement line 20? It's giving me errors on my end.
Is ".toCharArray()" Java code?
Method toCharArray() belongs to class String:
http://docs.Oracle.com/javase/8/docs/api/java/lang/String.html#toCharArray--
But if you've already got a
char[]
array, you can directly invoke the customized shuffle() function.Cool, I got it working :) .
If you can, would you be able to suggest a resource to help better understand the code that you referenced? I don't really know how it's sorting the array haha.
I haven't invented this algorithm. Merely adapted it to Java Mode & p5.js. :ar!
It's called Fisher-Yates Shuffle. Visit this link for it: http://Bost.Ocks.org/mike/shuffle/ :-bd