随机"走"在有限区域的中心位置?

sda*_*aau 10 java random algorithm math processing

我不确定我是否可以正确表达这个问题,但在这里它会...

我想编写一个例子,其中小点具有根据它们移动的速度 - 而且,有一个随机运动叠加到"正确"运动.使用Processing下面的代码,我得到以下动画:

marbles.gif

右边的点应该朝向右下角,我对它的行为表现不错.问题是左点,它应该是"静态的" - 所以它只会显示"随机"运动"到位"; 但是,正如动画.gif所示,它最终会偏离其原始位置一段距离.随机速度计算如下:

this.randspeed.set(random(0,1)-0.5, random(0,1)-0.5);
Run Code Online (Sandbox Code Playgroud)

我猜想这random(0,1)-0.5不会给我一个像高斯一样的"正态分布"(或者收敛到零); 但话又说回来,即使它是一个"正确的"高斯,我仍然可以有这样的"运气",所以说,正值[0:0.5]返回一整天,然后负值[-0.5:0]第二天返回,最后,它仍然是一个合适的高斯.

所以,我想,我正在寻找一种方法将(伪?) - 随机序列(如生成的那个)转换为伪随机序列random(0,1)-0.5,但其中N个样本的平均总和(例如10)我不知道如何称之为 - 一个随机序列周期性地收敛到零,我猜?

请注意,我一直在尝试position直接改变; 和保存position与改变finalpos,而不是-改变位置似乎更像是"自然的",平滑运动(特别是与模数帧的操作,所以一个新的随机速度没有分配每帧); 但是,它还允许随机噪声累积,并将点"推"远离其中心位置.另外,请注意我花了一些时间直到我可以在.gif上重现这一点,运行程序"live"似乎导致点更快地偏离原始位置(我已经阅读了一些关于硬件事件,如硬件磁盘写入用于/dev/random在Linux上更改熵,但我真的不知道它是否相关).

另外,我想到在点位置周围设置某种虚拟边框,并对从边界出来的随机运动进行碰撞检测 - 但在我看来,这对于太多的工作(以及矢量操作的CPU周期)来说似乎是这种事; 我希望随机函数能以某种方式以一种更容易的方式"缓和",而不是.

那么,是否有建议的方法在有限区域的中心位置周围进行这种随机运动?


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, height/2, 0, 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, 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;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Dam*_*ack 6

典型的"随机游走"将总是蜿蜒而去,因为统计数据不会"平衡".向左移动将不会通过向右移动来纠正.所以随机性的质量不是问题.

如果您希望点保持在特定位置,您应该存储该位置并使"正确"运动(如您所说)始终朝着该位置移动.从目标位置减去当前位置应该可以获得正确的"正确"运动.使用此解决方案,点将始终倾向于返回到开始的位置.