Copy an Array and Randomize the Order of Elements in the New Array?
in
Programming Questions
•
2 years ago
Is it possible to randomize the order of items in an array? I have an array list of the names of 16 7k images which I load into an array of PImages using loadImage in a loop in setup().
E.G.
What I'd like to do is draw the images in a different order than they are stored in the PImages array. I want to re-sort randomly the sprites array, e.g. make a copy of the sprites array, randomize the order of elements in the copy, and then instead of looping through the original array in draw, I loop through the copy with the randomized order.
I know how to copy an array. I just don't know if there's a clever/convenient way to randomize the order of elements.
Or, would it be better not to copy an array of PImages. Instead create a new integer array called spriteorders where each integer is the index of an image in sprites array and then do something like image(sprites[spriteorders[si]],0,0), if that's possible. Does creating a copy of an array of PImages bloat memory with additional loaded images or does it know the images are already loaded?
Or, instead of copying the arrays, I could do a "knuth shuffle" with something like SpritesRandom.add(sprites.remove(int(random(0,sprites.length)))) ; ??
Here's the full code: http://pages.suddenlink.net/tls/stevo_applet/ (not current but has all relevant parts). It throws an intermittent exeception java.net.MalformedURLException: no protocol: loading.gif on startup sometimes; refresh usually clears it.
E.G.
- String[] SpriteNames = {"sm8_rockstar.jpg","sm12_blonde.jpg","sm2_min.jpg","sm10_ozzy.jpg","sm3_pol.jpg","sm13_.jpg","sm7_guitar.jpg",
- "sm1.jpg","sm4_clown.jpg","sm5_glamor.jpg","sm6_int.jpg","sm9_stooges.jpg","sm11_glamor2.jpg","sm14_.jpg"
- };
- PImage[] sprites;
- <SNIP>
- void loadsprites() {
- sprites = new PImage[SpriteNames.length];
- for (int si = 0; si < SpriteNames.length;si++) {
- sprites[si] = loadImage(SpriteNames[si]);
- }
- }
What I'd like to do is draw the images in a different order than they are stored in the PImages array. I want to re-sort randomly the sprites array, e.g. make a copy of the sprites array, randomize the order of elements in the copy, and then instead of looping through the original array in draw, I loop through the copy with the randomized order.
I know how to copy an array. I just don't know if there's a clever/convenient way to randomize the order of elements.
Or, would it be better not to copy an array of PImages. Instead create a new integer array called spriteorders where each integer is the index of an image in sprites array and then do something like image(sprites[spriteorders[si]],0,0), if that's possible. Does creating a copy of an array of PImages bloat memory with additional loaded images or does it know the images are already loaded?
Or, instead of copying the arrays, I could do a "knuth shuffle" with something like SpritesRandom.add(sprites.remove(int(random(0,sprites.length)))) ; ??
Here's the full code: http://pages.suddenlink.net/tls/stevo_applet/ (not current but has all relevant parts). It throws an intermittent exeception java.net.MalformedURLException: no protocol: loading.gif on startup sometimes; refresh usually clears it.
1