简体   繁体   English

生成随机数,每次给出相同的数字

[英]Generating random numbers giving the same number each time

My assignment: 我的作业:

A simple random generator is obtained by the formula 𝑟𝑛𝑒𝑤 = (𝑎 ⋅ 𝑟𝑜𝑙𝑑 + 𝑏)%𝑚 . 通过公式𝑟𝑛𝑒𝑤 = (𝑎 ⋅ 𝑟𝑜𝑙𝑑 + 𝑏)%𝑚一个简单的随机发生器。 New “random” numbers are then generated by setting 𝑟𝑜𝑙𝑑 to 𝑟𝑛𝑒𝑤 and repeating the process. 然后,通过将𝑟𝑜𝑙𝑑设置为𝑟𝑛𝑒𝑤并重复该过程,可以生成新的“随机”数字。 Write a method that asks the user to enter a value for 𝑟𝑜𝑙𝑑, 𝑎, 𝑏 and 𝑚. 编写一个方法,要求用户输入𝑟𝑜𝑙𝑑,𝑎,𝑏和value的值。 Your method should return an array of integers that contain the first 25 “random” values generated by this formula. 您的方法应返回一个整数数组,其中包含此公式生成的前25个“随机”值。

So far this is what I have, but for some reason my code is not printing the array of random 25 numbers 到目前为止,这是我所拥有的,但是由于某种原因,我的代码未打印随机25个数字的数组

public static void main(String[] args){
        Scanner theInput = new Scanner(System.in);
        System.out.println("Enter a value for r: ");
        int r = theInput.nextInt();
        System.out.println("Enter a value for a: ");
        int a = theInput.nextInt();
        System.out.println("Enter a value for b: ");
        int b = theInput.nextInt();
        System.out.println("Enter a value for m: ");
        int m = theInput.nextInt();

        System.out.println(random(r,a,b,m));

    }

    public static int[] random(int r, int a, int b, int m){
        String num = "";
        int numberArray[] = new int [25];
        for (int i = 0; i < numberArray.length; i++) {
            int answer = (a*r+b)%m;
            numberArray [i] = answer;
        }
        for(int i=0;i<numberArray.length;i++){
            System.out.println(numberArray[i]);
        }
        System.out.println();
        return numberArray; 
    }

This is what is printing: 这是打印内容:

258
258
258
258
258
258
258
258
258
258
258
258
258
258
258
258
258
258
258
258
258
258
258
258
258

[I@55f96302

Can someone help me to fix the problem? 有人可以帮我解决问题吗?

Passing an array to System.out.println() will print out the "memory address" of the array. 将数组传递给System.out.println()将打印出数组的“内存地址”。 You can use Arrays.toString() to get a nicely formatted String of the array contents: 您可以使用Arrays.toString()获得格式正确的数组内容String

System.out.println(Arrays.toString(random(r,a,b,m)));
  1. You get 25 same numbers is because you are using the same r for the same formula 25 times! 得到25个相同的数字是因为您对相同的公式使用相同的r 25次!

Instead, according to the assignment requirements, you should update the r as the newly generated random number and use it to generate the next random number! 相反,根据分配要求,应将r更新为新生成的随机数,并使用它生成下一个随机数!

  1. You should use a for loop to print an array instead of System.out.println(numberArray) , see this for more explanation. 你应该用一个for循环输出数组代替System.out.println(numberArray)看到这个了更多的解释。

For your reference: 供你参考:

public static int[] random(int r, int a, int b, int m) {
    String num = "";
    int numberArray[] = new int [25];
    for (int i = 0; i < numberArray.length; i++) {
        int answer = (a * r + b) % m;
        numberArray [i] = answer;
        r = answer;                // you should set r as the answer and use it for the next random number
    }
    return numberArray;
}

public static void main(String[] args) {

    Scanner theInput = new Scanner(System.in);
    System.out.println("Enter a value for r: ");
    int r = theInput.nextInt();
    System.out.println("Enter a value for a: ");
    int a = theInput.nextInt();
    System.out.println("Enter a value for b: ");
    int b = theInput.nextInt();
    System.out.println("Enter a value for m: ");
    int m = theInput.nextInt();

    int[] numberArray = random(r, a, b, m);

    for(int i = 0; i < numberArray.length; i++){
        System.out.println(numberArray[i]);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM