Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
The last function I want to show you that deals with randomness in Processing is how to shuffle the items in an array. Now, you would think that this would be an easy thing to do, but unfortunately Processing does not have a built-in shuffle function and I've seen a number of people create rather long Byzantine complicated functions that can do shuffling. On the other hand, there is a shortcut for this and it is because Processing is based on Java and while Processing does not have a built-in shuffle function, Java does and so what I'm going to show is you is how to import a little bit of Java code and run it to shuffle items in an array.
Let's start by simply putting little comment up here at the top, with the sketch name and then I'm going to put -- I have to import a library and it's the Java utilities and so I write import and then java.util.* and that brings in all of the files within that particular folder and once I get that in, I can then define my array. Now I'm going to use a set of integers, but Java variable names work a little bit differently than in Processing, so if in Processing if I want an array of integers I would do int like that, but in Java, I have to do it slightly differently, I have to use a capital I and I have to spell integer out all the way.
Otherwise, it's the same. I do the opening and closing square brackets, I give the array a name, I'm I can going to call it nums for numbers and then I can do equals and then in curly brackets I'm just going to put five numbers, and so there's the array that I'm going to work with. We can just check how this array looks by using the print line command and you see at the bottom, I'll close this, we don't need that front of this one, we have 1, 2, 3, 4, 5. Now what I'm going to do is call on the special Java function and this one needs to be typed exactly as it appears. Again, capitalization is important.
We start by doing Collections and then dot (.) shuffle and then the thing that we are going to shuffle is the array, so we need to do parentheses and then we do capital A for Arrays and then dot (.) as capital L List and then I put the name of the array that I want to shuffle, in this case it's nums, I close that. That's the parentheses for the name of the array and then I close that for the function and then finish with the semicolon (;) and when I do that I can do print line again for nums, this will print it before and after shuffling you'll see how it looks and now it's printed out twice.
The first one shows the 1, 2, 3, 4, 5, the second time is 5, 4, 2, 3, 1 if I run it again I'll get a different shuffle. Then this 4, 2, 5, 3, 1 and so forth and so this is in fact the easiest way to shuffle an array in Processing and it also gives you tip about using Java. Now I should mention, if you want to get some really strong skills in Processing, using some Java would be a wonderful thing and Lynda.com has two courses that would be particularly useful in learning Java.
The first one is the Java Essential Training, and this would give you a lot of the information that you could then import into using with Processing. And the second one is Up and Running with Java Applications, and with the two of those, I'll be able to bring in some much stronger skills into Processing and have even a broader range of possibilities than you would otherwise.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 104268 Viewers
56 Video lessons · 116198 Viewers
71 Video lessons · 85415 Viewers
131 Video lessons · 40898 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.