Loading...
Logo
Processing Forum

Random number

in Programming Questions  •  6 months ago  
Hello everybody. I have coordinates for the position of an image stored in 4 different if's so it can appear in 4 different positions. What I want to do is to create a random number so it shows what's inside just one if.

The problem here is that random() generates lots of numbers and I only need the first one it generates.

Here's what I have.

Copy code
  1. int posXRandomItem=0;
  2. int posYRandomItem=0;
  3. int randomItem = (int) random(4);

  4. if (randomItem==1){
  5.  posXRandomItem=40;
     posYRandomItem=120;
  6. }

  7. if (randomItem==2){
  8.  posXRandomItem=200;
     posYRandomItem=120;
  9. }

  10. if (randomItem==3){
  11.  posXRandomItem=360;
     posYRandomItem=120;
  12. }

  13. if (randomItem==4){
  14.  posXRandomItem=520;
     posYRandomItem=120;
  15. }

  16. image(itemJustice, posXRandomItem, posYRandomItem);

But it shows the image in the 4 different positions every frame when running. How can I get it to show the image at just one position?

Replies(2)

Re: Random number

6 months ago
generate the random number only once outside of the draw, like in the setup

Re: Random number

6 months ago
Thanks.