Java: Can (new Random()).nextInt(5) always return the same number?

后端 未结 6 1622
温柔的废话
温柔的废话 2020-12-11 08:57

Sometimes this piece of code always returns the same number (and sometimes it works fine):

(new Random()).nextInt(5)

I have suspicions wher

相关标签:
6条回答
  • 2020-12-11 09:16

    The best way to approximate uniform distribution is to use the static method Random.nextInt(n) to produce integers in the range [0, n-1] (Yes, n is excluded). In your particular example, if you want integers in the range 0 to 5, inclusive, you would call Random.nextInt(6).

    0 讨论(0)
  • 2020-12-11 09:18

    ...Sometimes this piece of code [..] returns the same number (and sometimes it works fine)...

    So it works randomly??? :) :) :)

    Ok, ok, downvote me now!!

    0 讨论(0)
  • 2020-12-11 09:18

    The Javadoc for Random isn't explicit about this, but the seed it uses is probably dependent on the current system time. It does state the random numbers will be the same for the same seed. If you use the call within the same millisecond, it will use the same seed. The best solution is probably to use a static Random object and use it for subsequent calls to the method.

    0 讨论(0)
  • 2020-12-11 09:20

    If you're calling that line of code on successive lines, then yes, the two Random instances you're creating could be created with the same seed from the clock (the clock millisecond tick count is the default seed for Random objects). Almost universally, if an application needs multiple random numbers, you'd create one instance of Random and re-use it as often as you need.

    Edit: Interesting note, The javadoc for Random has changed since 1.4.2, which explained that the clock is used as the default seed. Apparently, that's no longer a guarantee.

    Edit #2: By the way, even with a properly seeded Random instance that you re-use, you'll still get the same random number as the previous call about 1/5 of the time when you call nextInt(5).

    public static void main(String[] args) {
        Random rand = new Random();
        int count = 0;
        int trials = 10000;
    
        int current;
        int previous = rand.nextInt(5);
    
        for(int i=0; i < trials; ++i)
        {
            current = rand.nextInt(5);
    
            if( current == previous )
            {
                count++;
            }
        }
    
        System.out.println("Random int was the same as previous " + count +
                           " times out of " + trials + " tries.");
    }
    
    0 讨论(0)
  • 2020-12-11 09:23

    The javadoc for java.util.Random is clear:

    If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers.

    The default constructor is also clear:

    Creates a new random number generator. This constructor sets the seed of the random number generator to a value very likely to be distinct from any other invocation of this constructor.

    In other words, no guarantees.

    If you need a more random algorithm, use java.security.SecureRandom.

    0 讨论(0)
  • 2020-12-11 09:29

    In Java 1.4, the default seed of of a new Random instance was specified in the API documentation to be the result of System.currentTimeMillis(). Obviously, a tight loop could create many Random() instances per tick, all having the same seed and all producing the same psuedo-random sequence. This was especially bad on some platforms, like Windows, where the clock resolution was poor (10 ms or greater).

    Since Java 5, however, the seed is set "to a value very likely to be distinct" for each invocation of the default constructor. With a different seed for each Random instance, results should appear random as desired.

    0 讨论(0)
提交回复
热议问题