关于数据挖掘:Java代码编写CS455-Random-Walk

24次阅读

共计 1101 个字符,预计需要花费 3 分钟才能阅读完成。

全文链接:tecdat.cn/?p=29602

Requirement
In this assignment you will write a graphics-based program to do a random walk, sometimes also known as a drunkard’s walk. This random walk simulates the wandering of an intoxicated person on a square street grid. The drunkard will start out in the middle of the grid and will randomly pick one of the four compass directions, and take a step in that direction, then another step from that new location in a random direction, etc.

This assignment will give you practice with creating classes, using loops, using the java library for random number generation, doing console-based IO, and drawing to a graphics window. Also you’ll get practice in general program development.

Analysis
Randow Walk,即随机游走问题,相似布朗运动,物体在下一刻走动的方向齐全是随机的。本题利用随机数生成器来模仿下一刻的方位。框架应用 java 的 awt 包实现了 UI 局部,咱们只须要将随机算法退出到提供的框架中。
解题的要害是理解随机数生成器的用法,依据提供的框架,找到对应函数入口,依据给定的测试集进行调试即可。

Tips
从测试函数入口动手

private void testTranslate(Impoint loc, int deltaX, int deltaY) {

ImPoint p2 = loc.translate(deltaX, deltaY);

}
复制代码
因而咱们须要实现 ImPoint 类,以及 translate 办法,外围代码如下

class ImPoint {
private int x;
private int y;

public Impoint translate(int deltaX, int deltaY) {

Random r = new Random();
int x = deltaX + nextInt(2) - 1;
int y = deltaY + nextInt(2) - 1;
return new Impoint(x, y);

}

}
复制代码

正文完
 0