이것을 테스트하기 위해 난수 발생을 100번 반복하여 난수값을 생성하는 새로운 메서드를 만든다.
public void testRandomInstances(SimpleKMeans kMeans) { Random rand = new Random(); for (int i = 0; i < 100; i++) { double x = rand.nextInt(200); double y = rand.nextInt(200); System.out.println(x + "/" + y + " test in cluster " + predictCluster(kMeans, x, y)); } }
위 메서드(testRandomInstances)는 x와 y의 난수를 만들고, 난수를 예측 메서드(predictCluster)로 전달한다. WekaCluster 생성자 내 센트로이드(showCentroids(kMeans, data);), 클러스터 출력(showInstanceInCluster(kMeans, data);), catch 블록 사이에 다음과 같이 한 줄을 추가한다.
testRandomInstances(kMeans);
프로그램을 다시 실행하면 다음과 같은 랜덤 테스트를 볼 수 있다.
146.0/167.0 test in cluster 1 109.0/67.0 test in cluster 1 95.0/80.0 test in cluster 3 29.0/160.0 test in cluster 5 165.0/193.0 test in cluster 1 33.0/167.0 test in cluster 5 108.0/73.0 test in cluster 1 63.0/63.0 test in cluster 2 186.0/176.0 test in cluster 1 67.0/47.0 test in cluster 2 43.0/5.0 test in cluster 2 85.0/9.0 test in cluster 2 152.0/60.0 test in cluster 1