Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 随机种子按预期产生不同的数目_Java_Random Seed - Fatal编程技术网

Java 随机种子按预期产生不同的数目

Java 随机种子按预期产生不同的数目,java,random-seed,Java,Random Seed,对于我的赋值,我需要使用Random(seed)生成一个包含不同4位数字的整数数组。请注意,generateSecretDigits方法是在课堂上开始的,但必须在家里完成(我记不起哪部分是在课堂上完成的) 我的教授给了我们一些例子来检验我们的方法是否有效。虽然我的程序生成了一个4位数的代码,但它显示的整数数组与所提供的示例不同。有人能帮我找到错误吗 提供的例子如下: GenerateCretDigits(45)返回数组{9,1,0,7} GenerateCretDigits(987)返回数组{

对于我的赋值,我需要使用Random(seed)生成一个包含不同4位数字的整数数组。请注意,
generateSecretDigits
方法是在课堂上开始的,但必须在家里完成(我记不起哪部分是在课堂上完成的)

我的教授给了我们一些例子来检验我们的方法是否有效。虽然我的程序生成了一个4位数的代码,但它显示的整数数组与所提供的示例不同。有人能帮我找到错误吗

提供的例子如下:

  • GenerateCretDigits(45)返回数组{9,1,0,7}
  • GenerateCretDigits(987)返回数组{5,8,9,7}
我的代码:

// Declare the required import statements

import java.util.Random;

public class BullsAndCows {
  public static void main (String[] args) {

    int[] y = generateSecretDigits(45);

    for (int i = 0; i < y.length; i++) {
      System.out.print(y[i] + " ");
    }

    System.out.println();
  }


  // A method that randomly generates a secret 4-digits number 

  public static int[] generateSecretDigits(int x) {

    /* Declare and initialize an array
     * Assign a default value that is not between 0 and 9 inclusively to each element of the array */

    int[] secretDigits = new int[4];
    secretDigits[0] = 10;
    secretDigits[1] = 10;
    secretDigits[2] = 10;
    secretDigits[3] = 10;

    // Generate a number between 0 and 9 inclusively with the provided seed

    int seed = x;
    Random digit = new Random(seed);

    for (int i = 0; i < secretDigits.length; i++) {

      int secret = digit.nextInt(9);

      // Assign a value to each element of the array

      /* The contains() method takes as input an array of integers and a 
       * specific integer. The method returns true or false depending if an 
       * element is contained within a given array. Note that I have tested 
       * the contains() method and it works perfectly */

      if (contains(secretDigits, secret) == false) {

        secretDigits[i] =  secret;
      }

      else {

        i--;
      }
    }

    return secretDigits;
  }
//声明所需的导入语句
导入java.util.Random;
公营牛群{
公共静态void main(字符串[]args){
int[]y=generateScretDigits(45);
对于(int i=0;i
Random
是一种使用种子、加法和乘法输出数字序列的方法。查看OpenJDK 11
Random
类,公式是:

nextseed = (oldseed * multiplier + addend) & mask;
其中,
乘法器
加数
掩码
是常数:

private static final long multiplier = 0x5DEECE66DL;
private static final long addend = 0xBL;
private static final long mask = (1L << 48) - 1;
私有静态最终长乘数=0x5deec66dl;
专用静态最终长加数=0xBL;
私有静态最终长掩码=(1L
Random
是一个使用种子、加法和乘法输出数字序列的函数。查看OpenJDK 11
Random
类,公式是:

nextseed = (oldseed * multiplier + addend) & mask;
其中,
乘法器
加数
掩码
是常数:

private static final long multiplier = 0x5DEECE66DL;
private static final long addend = 0xBL;
private static final long mask = (1L << 48) - 1;
私有静态最终长乘数=0x5deec66dl;
专用静态最终长加数=0xBL;

私有静态最终长掩码=(1L
nextInt(10)
要获得[0,9]范围,边界不为inclusive@KarolDowbecki我以为在使用nextInt(9)时,9已经包含在内了。谢谢lot@KarolDowbecki如果我使用nextInt(100)+1是范围[1100[?是的,您应该使用
nextInt(绑定)
若要获取范围,然后进行加法或减法运算以将其移动,例如,像您在
+1
nextInt(10)
中那样引入最小值若要获取[0,9]范围,则边界不为空inclusive@KarolDowbecki我以为在使用nextInt(9)时,9已经包含在内了。谢谢lot@KarolDowbecki如果我使用nextInt(100)+1是范围[1,100[?是的,您应该使用
nextInt(bound)
获取范围,然后加或减以移动范围,例如引入最小值,就像您使用
+1