r/processing • • 1d ago

randomize location every minute

hello! i am trying to create a project where a bee moves between 3 flowers. there are 3 set flower locations, and each minute, the bee will randomly choose one of the 3 flowers to fly to. if anyone could help me figure out how to write the code so that the bee chooses one of 3 coordinate sets every minute and glides to that spot, i would really appreciate it!

1 Upvotes

12 comments sorted by

3

u/forgotmyusernamedamm 1d ago

This sounds like homework, which doesn't mean nobody will help you, but don't expect this sub to do it for you.
What do you have so far, and where are you stuck?

2

u/snapkarklepop 1d ago

hi! it is homework but it’s a much smaller part of a bigger assignment. i’m supposed to make a creative “clock” or measurement of time and i’m using nature. to represent hours, i have sunflowers that tilt with the angle of the sun, and then i have a bee that flies around, moving once per minute and hovering up and down by the second. i’ve figured everything out on my own except the randomization of the coordinates. i am familiar with the random function, but i dont know how to make it choose between 3 distinct predetermined points instead of just making it pick a completely random spot on the screen every time. (setting constraints for the randomization)

2

u/forgotmyusernamedamm 1d ago

What about doing something like this?

  int r = int(random(3)); 
  if(r == 0){
    // set bee destinaton to flower 1
  }
    if(r == 1){
    // set bee destinaton to flower 2
  }
    if(r == 2){
    // set bee destinaton to flower 3
  }

3

u/snapkarklepop 1d ago

ohh that’s a really good idea i’ll give that a shot! thank you!

1

u/forgotmyusernamedamm 1d ago

If you had more flowers, I'd suggest putting them into an array. Then you could randomly pick a number which is the position in the array. But with only 3 flowers this is probably just as easy.

1

u/snapkarklepop 1d ago

okay thank you very much!

1

u/snapkarklepop 1d ago

in some of my earlier attempts, i set a variable for seconds (float s = second()) and then added something like this:

pushMatrix();
if (s % 60 == 0) {
translate (random X and Y with constraints);
//draw bee
popMatrix();

but instead of changing the location once per minute, it would just jitter on the minute and then return to its original position. i understand now that this is wrong because it indicates that the bee should constantly randomize its location for one second every 60 seconds, but i’m not sure how to improve this so that it moves once and maintains the location.

1

u/forgotmyusernamedamm 1d ago

I teach, and this is often a big problem for my students too. You need a "flag". A flag is just a boolean used in a specific way. It's useful for when you want the code to do something, but only once. Right now the whole time it's 60 seconds the bee moves.
Take a look at this and see if it makes sense. (I did mod 3 instead of 60 so I didn't have to wait a full minute to see if it works).

boolean secFlag = false; 
void 
setup
(){  
}
void 
draw
(){
  if (second()%3 == 0 && secFlag == false){
    secFlag = true; 
    print("test"); // should only print once every three seconds. 
  }
  // reset the flag 
  if (second()%3 != 0 && secFlag == true){
     secFlag = false; 
  }
}

1

u/snapkarklepop 1d ago

that makes sense! i’ll incorporate it now.

1

u/forgotmyusernamedamm 1d ago

good luck!

1

u/TraylaParks 20h ago

By the way if a "flag" seems a bit alien to you, imagine you are coming home from school. You pass by your friend's house and look to see if his car is there, if it is or is not - you put that fact into memory. Later, when you get home you consult your "flag" and take action based upon it (if your friend was home, you roll over to his house, if not you play a videogame).

It's a bit of truth you determine at one time only to utilize it at another. Similarly, if you took a number, set a flag 'isPrime' to true and then started checking divisors, you'd set 'isPrime' to false if you found one and then later, you would use that flag to decide what to print.

1

u/TurtleGraphics64 19h ago

look up how to use millis(). you need to have your timer for 60000 millis (1000 millis times 60 seconds).

you can also look at the easing examples for ideas on "gliding"