How can periodically randomise an integer?

edited March 2014 in How To...

Hi, i'd like to periodically(as in every x number of seconds) randomatise the integer 'direction' to give me a value between 0 and 3 so that the circles change direction and don't just get stuck in the walls... Does anyone know how to do this?

Cell[] Normal;

    void setup() {
      size(500, 500);
      Normal = new Cell [50];

      for (int i = 0; i < Normal.length; i++)
        Normal[i] = new Cell(random(0, 500), random(0, 500), int(random(0, 4)));
    }

    void draw() {
      background(255);
      stroke(0);
      strokeWeight(1);
      for (int i = 0; i < Normal.length; i++)
        Normal[i].desplay();
      for (int j = 0; j < Normal.length; j++)
        Normal[j].update();
    }

    class Cell {
      float xpos;
      float ypos;
      int direction;

      Cell(float tempXpos, float tempYpos, int tempDirection) {
        xpos = tempXpos;
        ypos = tempYpos;
        direction = tempDirection;
      }

      void desplay() {
        ellipse(xpos, ypos, 15, 15);
      }
      void update() {
       if(xpos < width && ypos < height && xpos > 0 && ypos > 0) {
        if(direction == 1) {
          ++xpos;
        }
        if(direction == 2) {
          --ypos;
        }
        if(direction == 3) {
          ++ypos;
        }
        if(direction == 0) {
          --xpos;
        }
        println(direction);
       }
      }
     }
Tagged:

Answers

  • I'm afraid to admit i can't make hide or hair of the formally addressed examples...? :-/

    Could you wright something simpler? I tried this using framerates... (line 52-56) For some reason it just stops after it's randomised 'direction' once... Any help you could offer?

    Cell[] Normal;
    int time = 0;
    
    void setup() {
      size(200, 200);
      Normal = new Cell [5];
    
      for (int i = 0; i < Normal.length; i++)
        Normal[i] = new Cell(random(0, 200), random(0, 200), int(random(0, 4)));
    }
    
    void draw() {
      background(255);
      stroke(0);
      strokeWeight(1);
      for (int i = 0; i < Normal.length; i++)
        Normal[i].desplay();
      for (int j = 0; j < Normal.length; j++)
        Normal[j].update();
        ++time;
    }
    
    class Cell {
      float xpos;
      float ypos;
      int direction;
    
      Cell(float tempXpos, float tempYpos, int tempDirection) {
        xpos = tempXpos;
        ypos = tempYpos;
        direction = tempDirection;
      }
    
      void desplay() {
        ellipse(xpos, ypos, 15, 15);
      }
      void update() {
       if(xpos < width && ypos < height && xpos > 0 && ypos > 0) {
        if(direction == 1) {
          ++xpos;
        }
        if(direction == 2) {
          --ypos;
        }
        if(direction == 3) {
          ++ypos;
        }
        if(direction == 0) {
          --xpos;
        }
       }
       if (time = 100) {
         direction = int(random(0, 4));
         println(time);
         time = 0;
        }
      }
    }
    
  • edited March 2014 Answer ✓

    In case you just wanted to change the direction of the balls then you could use something like this, no need for a timer.

    Cell[] normal;
    
    void setup() {
      size(500, 500);
      normal = new Cell [50];
    
      for (int i = 0; i < normal.length; i++) {
        normal[i] = new Cell(random(1, width), 
          random(1, height), 
          random(-2, 2),
          random(-2, 2)
          );
      }
      stroke(0);
      strokeWeight(1);
    }
    
    void draw() {
      background(255);
    
      for (int i = 0; i < normal.length; i++) {
        normal[i].move();
        normal[i].display();
        normal[i].update();
      }
    }
    
    class Cell {
      float xpos;
      float ypos;
      float speed;
      float xSpeed;
      float ySpeed;
    
      Cell(float _xpos, float _ypos, float _speed) {
        xpos = _xpos;
        ypos = _ypos;
        speed = _speed;
      }
      Cell(float _xpos, float _ypos, float _xSpeed, float _ySpeed) {
        xpos = _xpos;
        ypos = _ypos;
        xSpeed = _xSpeed;
        ySpeed = _ySpeed;
      }
      void move() {
        if(speed != 0) {
          xpos += speed;
          ypos += speed;
        } else {
          xpos += xSpeed;
          ypos += ySpeed;
        }
      }
      void update() {
        if(speed != 0) {
          if (xpos > width || xpos < 0) {
            speed *= - 1;
          }
          if (ypos > height || ypos < 0) {
            speed *= - 1;
          }
        } else {
          if (xpos > width || xpos < 0) {
            xSpeed *= - 1;
          }
          if (ypos > height || ypos < 0) {
            ySpeed *= - 1;
          }
        }
      }
      void display() {
        ellipse(xpos, ypos, 15, 15);
      }
    }
    

    Few notes of advice as well. Always close your for() cycles. Objects by convention are written with lower case. Also you can add multiple constructors. To change the direction you need to invert the speed of the balls. That is done with speed *= -1.

Sign In or Register to comment.