首页 > 解决方案 > 保存并重新使用这个“随机”?

问题描述

出于某种原因,我创建的 Random 对象出现错误,我看不出任何原因。有人可以帮忙吗?我将不胜感激!

public static void experiment(String method, String testType, int arraySize, int trials){
    int[] intArray = new int[arraySize];
    Random rand = new Random();
    Long startTime;
    Long endTime;
    Long duration;
    String durationStr;
    String arraySizeStr = String.valueOf(arraySize);
    for (int i =0; i<arraySize;i++){
        intArray[i] = i;
    }
    if (method=="exhaustive"){
        if (testType =="worst"){
            for (int j = 0; j<= trials; j++){
                startTime = System.currentTimeMillis();
                exhaustive(intArray, intArray[arraySize-1]*3);
                endTime = System.currentTimeMillis();
                duration = endTime - startTime;
                durationStr = duration.toString();
                System.out.println("Exhaustive, Worst, " + durationStr + ", Array Size: " + arraySizeStr);
            }
        }
        else{
            for (int j = 0; j<= trials; j++){
                startTime = System.currentTimeMillis();
                exhaustive(intArray, rand.nextInt((arraySize -1) * 2));
                endTime = System.currentTimeMillis();
                duration = endTime - startTime;
                durationStr = duration.toString();
                System.out.println("Exhaustive, Best, " + durationStr + ", Array Size: " + arraySizeStr);
            }
        }
    }

标签: javarandom

解决方案


推荐阅读