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 & HelpPrograms › displaying a random image
Page Index Toggle Pages: 1
displaying a random image (Read 1783 times)
displaying a random image
Nov 22nd, 2009, 12:47pm
 
Hello everyone!

I am trying something and have been browsing around here and trying some stuff. Till now i've been all succesfull

Now I am stuck with a really simple thing I want
I am trying the following:

I have like 10 images but each time I want to run the program I just want one image out of these 10 to load at a certain spot but the coordinates are easy ofcourse.

The names of the images are all diffrent not like c c1 c2 etc... but more like keyboard, mouse, monitor for example

anyways thanks for reading
and kudos if you can help me out

I have consulted this website and ofcourse google as well before asking here. So please refrain from google is your friend and that kinda stuff



Re: displaying a random image
Reply #1 - Nov 22nd, 2009, 1:22pm
 
just create an array of images and randomly pick one using (int)random(x);

PImage[] images = new PImage[numImages]; //image array

images[0] = loadImage("monitor.jpg");
images[1] = loadImage("screen.jpg");
images[2] = loadImage("whatever.jpg");
images[3] = loadImage("andever.jpg");  
etc ...
Re: displaying a random image
Reply #2 - Nov 23rd, 2009, 12:32am
 
kudos

thnx
Re: displaying a random image
Reply #3 - Nov 24th, 2009, 12:01am
 
Cedric wrote on Nov 22nd, 2009, 1:22pm:
just create an array of images and randomly pick one using (int)random(x);



You could also use the random() function. However, you would need to know how many images are in your array, which would require another function before that. Also keep in mind that the random is not a integer, so you could use round(random(etc etc))).
Re: displaying a random image
Reply #4 - Nov 24th, 2009, 1:38am
 
Quote:
You could also use the random() function.


thats actually what i do. I use the random function to pick a random number between 0 and x where x should be the array.length ( no need to use a function for that)
and by putting (int) there i already translated the float to an int....
if you would use round you can not use the numbers of images in random cause that could cause an error. you have to do numer of images -1 then.
Re: displaying a random image
Reply #5 - Nov 24th, 2009, 1:50am
 
Justin wrote on Nov 24th, 2009, 12:01am:
Cedric wrote on Nov 22nd, 2009, 1:22pm:
just create an array of images and randomly pick one using (int)random(x);



You could also use the random() function. However, you would need to know how many images are in your array, which would require another function before that. Also keep in mind that the random is not a integer, so you could use round(random(etc etc))).


Code:
 (int)random(x); 

is the random() function.  the '(int)' at the beginning casts the result as an int, which by all accounts is a little quicker than converting to an int.

Also you can get the length of an array with 'array.length'.
Page Index Toggle Pages: 1