This site requires JavaScript, please enable it in your browser!
Greenfoot back
patrick26
patrick26 wrote ...

2014/3/9

Random Images

patrick26 patrick26

2014/3/9

#
Hi, I am very new to computer programming and need some help with knowing how to randomly select one of three images I have. Thanks.
bourne bourne

2014/3/9

#
Are the 3 images inside an array or List? Which case you could do something like this:
images[(int)(images.length * Math.random())]; // For array
images.get((int)(images.size() * Math.random()); // For List
Or you could do something like this:
int randomRoll = (int)(3 * Math.random());
if (randomRoll == 0)
    image1;
else if (randomRoll == 1)
    image2;
else
    image3;
You need to login to post a reply.