Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

java - Random generator giving me the same number everytime

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



}



}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();

That

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()


Why?

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.

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!

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 - 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."


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...