随机发生器每次给我相同的数字 [英] Random generator giving me the same number everytime

查看:129
本文介绍了随机发生器每次给我相同的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.util.Random;

public class Fighter {
int str;
int dex;
int hth;
Random gen = new Random(1535636);   

public Fighter() {
     str = gen.nextInt(9);
     dex = gen.nextInt(9);
     hth = gen.nextInt(14);
}


public int getHth(){

   return hth;

   }

public int getStr(){
   return str;
   }

public int getDex(){
   return dex;
       }
 }


import java.util.Random;
public class Arena {


public static void main(String[] args) {
    Random gen = new Random();
      Fighter Guy1 = new Fighter();
      int x =1;
              while (x < 200000000){
                x++;  
              }
      Fighter Guy2 = new Fighter();

    int hth1 = Guy1.getHth();
    int hth2 = Guy2.getHth();

    System.out.println("Fight Start");
    System.out.println("---------------");
    //System.out.println(gen.nextInt(10));
    //System.out.println(gen.nextInt(17));
    System.out.println(Guy1.getStr());

    //Fighting



}



}

无论什么时候运行,我都会得到相同的结果。我希望每次都能制作2个随机战士。现在有几行只是为了确认它不会产生随机数。

Whenever i run this i get the same results no matter what. I'd like it to make 2 random fighters each time. Right now theres a few lines that were just to confirm that it doesn't make random numbers.

有没有人知道如何在构造函数中正确使用随机数?或者我完全错了吗?

Does anyone know how to use random numbers in a constructor properly? or am i doing this completely wrong?

推荐答案

您为随机数生成器提供了一个常量种子值:

You're providing a constant seed value to the random number generator:

Random gen = new Random(1535636);  

不要这样做。它将始终提供相同的值。只需调用默认构造函数:

Don't do that. It will always provide the same values. Just call the default constructor:

Random gen = new Random();


创建一个新的随机数生成器。它的种子初始化为基于当前时间的值:

Creates a new random number generator. Its seed is initialized to a value based on the current time:

http://docs.oracle.com/javase/1.4.2/docs/api/java /util/Random.html#Random()

为什么?

与大多数标准库随机数生成器一样, Random 伪随机数生成器。这意味着它不是实际上生成随机数!相反,它是以一种非常明确的方式计算它们 - 它们只是像随机数一样看起来,并且它们往往具有良好的分布。

As with most "standard library" random number generators, Random is a "Pseudorandom number generator". That means it is not actually generating random numbers! Instead, it is calculating them in a very defined fashion - they just look like random numbers, and they tend to have a decent distribution.

PRNG使用种子值进行初始化,该值设置其内部状态。如果每次都提供相同的种子值,PRNG每次运行时都将处于相同的状态,因此,提供相同的数字序列!

PRNGs are initialized with a seed value which sets their internal state. If you provide the same seed value every time, the PRNG is going to be in the same state every time you run it, and thus, provide the same sequence of numbers!

使它们看起来随机的东西,通常是 1 它们默认是播种的,具有基于时间的值。在大多数库中,这是一个具有非常精确的时间值。所以大部分时间,如果你看到有人播种他们的PRNG,那可能是不正确的,或者至少是非常不必要的。

The thing that makes them seem random all the time, is that usually1 they are "seeded" by default with a time-based value. In most libraries this is a time-dervied value with very good precision. So most of the time, if you see someone seeding their PRNG, it is probably incorrect, or at least very unnecessary.

1 - 注意:这是 not 来自libc的 rand()的情况:如果没有提供种子值,则rand()函数会自动播种值为1.

1 - Note: This is not the case with rand() from libc: "If no seed value is provided, the rand() function is automatically seeded with a value of 1."

这篇关于随机发生器每次给我相同的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆