동일한 매개 변수로 여러 번 반복하여 개체를 만들고 있습니다. 이 객체에는 임의의 메소드 (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;
}
}
지금까지 내가 말할 수있는
일부 코드가 맞습니까? – Damiano
[mcve]를 포함하십시오. – erickson
이걸 디버그하기 위해서'Edge' 클래스에서 더 많은 것을 볼 필요가 있습니다. –