We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › randomize i in a for() construction
Page Index Toggle Pages: 1
randomize i in a for() construction (Read 1153 times)
randomize i in a for() construction
Mar 24th, 2008, 11:26am
 
I have used the collage-code from the Processing book to create my own collage. I loaded all images in an PImage[] array and made code more compact. But I want to "randomize" the order in which the images are loaded.

Now, every "page" holds the images in the same order. I want image 1 to appear on top of the stack as well. Here's the part of the code I'm reffering to:

Code:
page=new PImage[28];
for(int i=0;i<page.length;i++) {
page[i]=loadImage("nyt_"+i+".jpg");
image(page[i], -page[i].width/2, -page[i].height/2);
}
Re: randomize i in a for() construction
Reply #1 - Mar 26th, 2008, 3:22am
 
You could try generating a random int, insert that into a hash until that hash size is the size you want, and iterate through that as a sequence for the loading of images.

i'm not sure if unique random numbers in a range is already possible in java.
Re: randomize i in a for() construction
Reply #2 - Mar 26th, 2008, 8:25am
 
Somehow it makes sense, but can you give an example?

I'm not a programmer and I still need examplecode to modify.
I'm glad someone takes time to help me out. Thanks.
Re: randomize i in a for() construction
Reply #3 - Mar 26th, 2008, 10:28am
 
off the top of my head in pseudo code maybe like this

while(x!=whatever-1){
int a=int(random(0,whatever));
if(!exists(a) in hash){
 insert a in hash;
x++;
}
}

voila ... iterate thru that hash and load em up.
Re: randomize i in a for() construction
Reply #4 - Mar 26th, 2008, 12:18pm
 
This is the complete code:
I tried to use your example, but it failed to execute.
Code:
float x, y, r, s; 
PImage[] page;
void setup(){
size(800,600);
smooth();
background(0);
}
void draw(){
fill(0);
rect(0,0,width,height);
noFill();
page=new PImage[28];

// while(x!=whatever-1){
// int a=int(random(0,whatever));
// if(!exists(a) in hash){
// insert a in hash;
// x++;
// }}

for(int i = 0; i < page.length;i++) {
page[i]=loadImage("nyt_"+i+".jpg");
x = random(width);
y = random(height);
s = random(0.4,0.8);
tint(255, 220);
r = random(-0.2,0.2);
pushMatrix();
translate(x,y);
rotate(r);
scale(s);
image(page[i], -page[i].width/2, -page[i].height/2);
popMatrix();
}
saveFrame("page_" + frameCount + ".jpg");
if (frameCount == page.length) {
exit();
}
}

Re: randomize i in a for() construction
Reply #5 - Mar 26th, 2008, 7:47pm
 
move the initialisation of the page loop into the setup and it'll be much faster. you don't need load all the images on every loop.

for the randomness i'd have an array filled with 0 to n. then do a knuth shuffle on it and use shuffled_array[i] instead of i in your image() call.

http://en.wikipedia.org/wiki/Fisher-Yates_shuffle

(it's about 5 lines of code)
Re: randomize i in a for() construction
Reply #6 - Mar 26th, 2008, 10:06pm
 
thanks!
This is helpfull. All the random-stuff I tried ended up with a lot of processor/harddisk-noise from my pc.

I'll do the knuth-shuffle tommorrow.
And the Wallstreet-shuffle next year... Wink
Re: randomize i in a for() construction
Reply #7 - Mar 27th, 2008, 2:51am
 
damn that's a good algorithm, i must get that book.

pseudo code means it's not actual running code. It's just a easy method of verbalizing the algorithm without getting into the nitty gritty details of the syntax.

Re: randomize i in a for() construction
Reply #8 - Mar 27th, 2008, 8:17am
 
Indeed. It's a pretty nice algorithm the Knuth shuffle. I've never heard of it before, but it seems to be pretty intuitive.

I'll have to find some applications for it then.

Yey. :]
Page Index Toggle Pages: 1