How to exclude values in a variable and reset the program to start with a single click mouse

edited February 2016 in How To...

Hello,

I would like to know how to exclude values in a variable when it takes values in a random () function. For example my variable y takes values randomly between 20 and 780 or y = (random (20,780)) However I do not want the variable y takes values between 300 and 400. Basically I want a function that prohibits y to take value between 300 and 400 but the rest yes. In addition I also search a function to initialize the program at the beginning when I press my mouse with a simple click. Indeed when I click on run, the program starts on a page 1 and when I finished my program, the program shows me on page 2. So on the page 2 when I click with my mouse, it starts again like at the beginning. If it's possible of course.

Thank you.

Answers

  • edited February 2016
    // forum.Processing.org/two/discussion/14883/
    // how-to-exclude-values-in-a-variable-
    // and-reset-the-program-to-start-with-a-single-click-mouse
    
    // GoToLoop (2016-Feb-12)
    
    void setup() {
      noLoop();
    }
    
    void draw() {
      int y = (int) (random(1) < .5? random(20, 300) : random(400, 780));
      print(y, TAB);
    }
    
    void mousePressed() {
      redraw();
    }
    
    void keyPressed() {
      redraw();
    }
    
  • edited February 2016 Answer ✓
    // forum.Processing.org/two/discussion/14883/
    // how-to-exclude-values-in-a-variable-
    // and-reset-the-program-to-start-with-a-single-click-mouse
    
    // GoToLoop (2016-Feb-12)
    
    void setup() {
      noLoop();
    }
    
    void draw() {
      int y;
      while ((y = (int) random(20, 780)) >= 300 & y < 400);
      print(y, TAB);
    }
    
    void mousePressed() {
      redraw();
    }
    
    void keyPressed() {
      redraw();
    }
    
  • edited February 2016
    // forum.Processing.org/two/discussion/14883/
    // how-to-exclude-values-in-a-variable-
    // and-reset-the-program-to-start-with-a-single-click-mouse
    
    // GoToLoop (2016-Feb-12)
    
    void setup() {
      noLoop();
    }
    
    void draw() {
      int y = (int) random(20, 780, 300, 400);
      print(y, TAB);
    }
    
    void mousePressed() {
      redraw();
    }
    
    void keyPressed() {
      redraw();
    }
    
    float random(float minRange, float maxRange, float noMinRange, float noMaxRange) {
      float rnd;
      while ((rnd = random(minRange, maxRange)) >= noMinRange & rnd < noMaxRange);
      return rnd;
    }
    
  • Thank you very much, it works on my program. I prefer the solution 2 with : while ((y = (int) random(20, 780)) >= 300 & y < 400);

    And for reset it works also :)

This discussion has been closed.