function that redo random for specific combination

edited March 2016 in Questions about Code

I don't know if it has a function for this. Similar to constrain() but the opposite , the number cannot go there.

int xImg= int(random (0, 8))*int(-144);  // grid X
int yImg=int(random (0, 6))*int(144);  // grid y
int sImg =int(random(1, 5))*int(72); // icon size

so I don't want to have those combinations (xImg,yImg) (4,1) (5,1)(6,1)(7,1)(8,1)

if (yImg==1 &&  xImg>=4 &&  xImg<=8){
redo random until the number is not == to (yImg==1 &&  xImg>=4 &&  xImg<=8)
}

thanks

Answers

  • so if i understand it wil be something like that

    int xImg
    int yImg
    int sImg
    
    sImg =int(random(1, 5))*int(72); // icon size
    
    do xImg= int(random (0, 8))*int(-144);  // grid X
    do yImg=int(random (0, 6))*int(144);  // grid y
     while (yImg==1 &&  xImg>=4 &&  xImg<=8);
    
  • edited March 2016

    Just by taking a quick glance on your code above, it's pretty obvious it won't compile!
    It's a do...while pair, not just 1 single do. Please make an effort to have a compilable code to post.

  • edited March 2016
    // forum.Processing.org/two/discussion/15723/
    // function-that-redo-random-for-specific-combination
    
    // GoToLoop (2016-Mar-29)
    
    int x, y;
    
    void setup() {
      noLoop();
    }
    
    void draw() {
      background((color)random(#000000));
      print(x, y, TAB);
      assert y != 1 | y == 1 & (x < 4 | x > 8);
    }
    
    void keyPressed() {
      y = (int)random(6);
      if (y != 1)  x = (int)random(8);
    
      else do x = (int)random(8);
      while (x >= 4);
    
      redraw();
    }
    
    void mousePressed() {
      keyPressed();
    }
    
  • void keyPressed() {
      y = (int)random(6);
      if (y != 1)  x = (int)random(8);
      else while ((x = (int)random(8)) >= 4);
      redraw();
    }
    
  • edited March 2016 Answer ✓

    you don't need a do while loop in this case - just calculate y first and use a different range for x if y is 1

    I don't want to have those combinations (xImg,yImg) (4,1) (5,1)(6,1)(7,1)(8,1)

    y = (int)random(6);
    if (y == 1) {
      x = (int)random(4); // 0 to 3
    } else {
      x = (int)random(8); // 0 to 7
    }
    

    NB (int)random(n) will return a random number from 0 to n-1

Sign In or Register to comment.