【问题标题】:How to create random numbers a specific number of times?如何创建特定次数的随机数?
【发布时间】:2020-09-17 19:21:00
【问题描述】:

如何创建一个特定时间的随机数?

public class Feld  {
    public static void main(String[] args) {
        double k = (int)(Math.random()*1000001);
        int n = 1000000;
        int arr[] = new int[n];
        int i = 0;
        for(i = 0;i<n;i++){
            arr[i] = i;
        }
        boolean found = false;
        i=0;
        while (i < arr.length) {
            if (arr[i] == k) {
                found = true;
                break;
            }
            i++;
        }
        if (found) {
            i++;
            System.out.println(i);
        } 
        else {
            System.out.println((arr.length + 1));
        }
    }
}

我的问题是,如果我将 k 放入一个循环中多次创建它,我将在以下位置收到错误:

if (arr[i] == k)

!!我刚刚发现我在解释我的问题时犯了一个错误。该数组应填充 0-1.000.000 之间的值,并且我应该在特定次数内打印出随机生成数字的位置。

【问题讨论】:

    标签: java loops random


    【解决方案1】:

    如果你想要一个充满随机数的数组,我建议使用以下方法:

    int n = 1000000;
    int arr[] = new int[n];
    for(int i = 0; i < n; i++){
        arr[i] = (int)(Math.random() * 1000001);
    }
    

    这会起作用,你甚至不需要变量k


    编辑:

    如果要打印在什么位置找到特定值(例如x = 543),可以使用以下代码:

    int x = 543;
    int n = 1000000;
    int arr[] = new int[n];
    for(int i = 0; i < n; i++){
        arr[i] = (int)(Math.random() * 1000001);
        if(arr[i] == x) {
            System.out.println(i);
            break;
        }
    }
    

    编辑2

    您的新问题的一种可能解决方案如下所示:

    public class Feld  {
        public static void main(String[] args) {
            int n = 1000000;
            int arr[] = new int[n];
            int i = 0;
            for(i = 0; i < n; i++){
                arr[i] = i; //Filling array with values 0-1000000
            }
            int number = 20;    //Print out position of a random generated number a specific amount of times
            int randomNumber = (int)(Math.random()*1000001); //The random number
    
            for(int j = 0; j < number; j++) { //Find number for a specific amount of times 
                for(int k = 0; k < arr.length; k++) { //Find number in array
                    if(arr[k] == randomNumber) { 
                        System.out.println(arr[k]); //Print
                        break; //Number found, don't have to search anymore
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 数组的长度应始终为 1000000 但我必须使用不同数量的值并测量输入它们需要多长时间,例如 10k 值 100k、1m、10m ...我应该用 k 做什么,我必须在这个 for 循环中创建另一个 for 循环吗?数组不必完全填满。
    • 我刚刚编辑了我的答案。这能回答你的问题吗?
    • 我认为它可以工作,只是我应该使用上面代码中的结构。只是我用特定数量的随机数填充数组。并且数字应该通过算法抛出。如果我将 1 个数字、10k、100k、1m、10m 放入数组中,我必须测量它需要多长时间。我还没有实现代码来测量时间。我想在代码正常工作之后再做。
    • 好的。 (1)用特定数量的数字填充数组,可以使用第一个代码sn -p。您只需将变量 n 更改为所需的数字。 (2) 您将无法在大小为 1m 的数组中填充 10m 个数字。
    • 我知道我也想知道它应该如何工作它必须是任务中的一个错误。
    【解决方案2】:

    我会编写一个方法,该方法返回一个随机数数组并接受一个定义数组长度的int 参数。

    一种可能的解决方案是:

    public static int[] createRandomArray(int length) {
        // create an array of the given length
        int[] result = new int[length];
        // and use a single for loop that puts random int values into every index
        for (int i = 0; i < result.length; i++) {
            result[i] = ThreadLocalRandom.current().nextInt();
        }
        // then simply return the result
        return result;
    }
    

    如下尝试

    public static void main(String[] args) {
        // super primitive time measurement:
        // take the moment in time before calling the method
        Instant start = Instant.now();
        // then call the method
        int[] array = createRandomArray(1000000);
        // and take the moment in time after the method returned
        Instant end = Instant.now();
        // then calculate the duration
        Duration duration = Duration.between(start, end);
        // and print the duration in milliseconds
        System.out.printf("Array creation took %d milliseconds\n", duration.toMillis());
    }
    

    结果是我系统上的以下输出:

    Array creation took 10 milliseconds
    

    【讨论】:

    • 数组长度假定为1m,但不必完全填充。
    • @M.Eichhorn 你可以在这里插入 1000000 而不是 10...无论如何它都会被填充,因为如果没有指定不同的值,每个索引都将被初始化为 0。
    猜你喜欢
    • 2015-07-02
    • 2013-03-07
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 2015-07-21
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多