Java – randomly “walk” near the center of a limited area

I don't know if I can correctly express this problem, but here

I want to write an example where the dots have a speed according to their movement - and there is a random motion superimposed on the "correct" motion Using the following processing code, I get the following Animation:

The correct point should be in the lower right corner. I can determine its behavior The problem is the left point, which should be "static" - so it will only show "random" action "in place"; However, just like animation Gif shows that it often ends up a distance from its original position The random velocity is calculated by the following formula:

this.randspeed.set(random(0,1)-0.5,random(0,1)-0.5);

I will guess that random (0,1) - 0.5 does not give me the "normal distribution" of Gaussian state centered on "zero"; But again, even if it is an "appropriate" Gauss, I can still have such "luck", so a positive value [0:0.5] returns all day, and then a negative value [- 0.5:0) returns the next day. Finally, it is a correct Gauss

So, I think I'm looking for a way to convert (pseudo) random sequences (generated as random (0,1) - 0.5) into pseudo-random sequences, but the average sum of n samples (e.g. 10) is 0.5 I don't know how to call this - the random sequence converges to zero periodically, I guess?

Please note that I have been trying to change the position directly below; And change the final save position - changing the position seems more like a "natural" smooth motion (especially analog-to-digital operation, so each frame is not assigned a new random speed); However, it also allows random noise accumulation and "pushes" the point to its center In addition, please note that I spent a few hours until I could be in To reproduce this on GIF, running the program "live" seems to make the point deviate from its original position faster (I've read about hardware events, disk write entropy for changing / dev / random on Linux, but I don't know if it's relevant)

In addition, I wanted to set some virtual frames around the point position and detect the collision of the random motion in the frame, but in my opinion, it seems to be too much work (and the CPU cycle of vector operation). I had hoped that the random function could be "reconciled" in a simpler way in some way

So, is there a recommended method for this random motion of the center in a limited area?

marbles. pde:

import java.util.*; // added for Iterator;

ArrayList<Marble> marbles = new ArrayList<Marble>();
Iterator<Marble> imarb;
color mclr = #0000FF;
int RNDLIMIT = 2;
int framecount = 0;

void setup() {
  size(600,400,P2D);
  Marble m_moving = new Marble(width/2,height/2,2,2);
  marbles.add(m_moving);
  Marble m_stopped = new Marble(width/2-100,0);
  marbles.add(m_stopped);
}

void draw() {
  background(255);

  strokeWeight(1);
  stroke(mclr);
  fill(mclr);

  imarb = marbles.iterator();
  while (imarb.hasNext()) {
    Marble m = imarb.next();
    m.update();
    ellipse(m.finalpos.x,m.finalpos.y,m.radius*2,m.radius*2);
  }
  framecount++;
  //~ saveFrame("marbles-######.png");
}

class Marble {

  PVector position = new PVector(0,0);
  PVector finalpos = new PVector(0,0);
  PVector speed = new PVector(0,0);
  PVector randspeed = new PVector(0,0);
  float radius=4;

  public Marble() {
  }

  public Marble(float inx,float iny,float invx,float invy) {
    this.position.set(inx,iny);
    this.speed.set(invx,invy);
  }

  public void update() {
    this.position.add(this.speed);
    if (framecount % 4 == 0) {
      this.randspeed.set(random(0,1)-0.5);
      this.randspeed.setMag(RNDLIMIT);
    }
    int algoTry = 1; // 0
    switch(algoTry) {
      case 0:
        this.finalpos.set(PVector.add(this.position,this.randspeed));
        break;
      case 1:
        this.position.set(PVector.add(this.position,this.randspeed));
        this.finalpos.set(this.position);
        break;
    }
  }
}

Solution

A typical "random walk" will always meander through because the statistics are unbalanced Moving a lot to the left will not correct the right movement So the randomness of quality is not a problem

If you want the point to stay in a specific position, you should store that position and make the "correct" action (as you said) to move all the way to that position Subtracting the current position from the target position should result in the correct "correct" motion With this solution, the point will always tilt back to where it started

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>