【发布时间】:2016-07-28 18:38:48
【问题描述】:
我一遍又一遍地多次创建具有相同参数的对象。该对象有一个随机方法(使用 Math.random()),我知道它每次都应该返回一个不同的数字,但是每次在程序中我创建该类的一个新对象并在其上调用该方法时,它都会返回相同的值。我应该如何解决这个问题?
我调用方法契约的地方:
for (int i = 0; i < 212000; i++){
Contractions c = new Contractions(a, b);
temp = c.contract();
if (temp < min){
min = temp;
}
if (i%1000 == 0){
System.out.println(min);
}
}
方法:
while (vertices.size() > 2){
Edge randEdge = edges.get((int) (Math.random()*edges.size()));
vertices.remove(randEdge.getTwo());
for (int i = edges.size() - 1; i >= 0; i--){
if (edges.get(i).getOne() == randEdge.getTwo()){
edges.get(i).setOne(randEdge.getOne());
}
if (edges.get(i).getTwo() == randEdge.getTwo()){
edges.get(i).setTwo(randEdge.getOne());
}
}
edges.remove(randEdge);
removeSelfLoops();
return edges.size();
边缘类:
package Contractions;
public class Edge {
Vertex one;
Vertex two;
public Edge(Vertex one, Vertex two){
this.one = one;
this.two = two;
}
public boolean isEqual(Edge other){
if (other.one == this.one && other.two == this.two){
return true;
}
if (other.two == this.one && other.one == this.two){
return true;
}
return false;
}
public Vertex getOne(){
return one;
}
public Vertex getTwo(){
return two;
}
public void setOne (Vertex v){
one = v;
}
public void setTwo (Vertex v){
two = v;
}
public String toString(){
return one + "; " + two;
}
}
【问题讨论】:
-
我们可能需要从您的
Edge类中查看更多信息才能调试此问题
标签: java eclipse random runtime-error