What is the use of the Random(long) constructor?

前端 未结 5 2079
我寻月下人不归
我寻月下人不归 2021-01-22 04:04

There are 2 constructors of Random class

  1. public Random()
  2. public Random(long seed)

The description fo

相关标签:
5条回答
  • 2021-01-22 04:14

    This is a fun one! The Random number generation is not random at all. If you use the same seed and ask it for a bunch of random numbers, you will get the same sequence. This is important as it allows different computers to predictably generate the same sequence as long as they have shared the seed. If you do not specify a seed, then one is chosen for you that is very unlikely to be chosen by any other VM in the world. BUT, if someone were to guess the seed you used, they would be able to generate the same sequence of numbers.

    From Google search:Random search

    0 讨论(0)
  • 2021-01-22 04:14

    The Random generator is a Pseudorandom number generator. That means that it is, in fact, not a random number generator but only some clever algorithm that produces fully deterministic numbers that just look like random.

    When the generator is used, each time it produces a random number it modifies its internal state in order to produce a different number at the next call. However, at the beginning the internal state of this algorithm must be initialised, and the value that is used for this initialisation is commonly called seed. The parameterless constructor makes a seed on its own, based on system time, while the other constructor lets you to put your seed, which allows you to make it repeatable - the same seed (and the same generator) will produce the same sequence of numbers.

    If you are interested, here is the source code of the Random class from OpenJDK (i.e. the open source implementation of Java, but it should be functionally equivalent). The constructor with seed is at line 135, the setSeed method is at line 168 and e.g. the nextInt method is at line 328 which just calls a private method next which is at line 198 and which is where all the magic happens. It also has javadoc with the reference to the (probably more mathematical) description of this kind of generator.

    0 讨论(0)
  • 2021-01-22 04:15

    If you use the constructor with the seed, you will get a repeatable sequence, so it's good for testing. If you use the constructor without the seed, you don't know what sequence of random-like numbers will be produced.

    0 讨论(0)
  • 2021-01-22 04:29

    I wonder whether what you want to ask is about distinguishing the default constructor and argument constructor. When you instantiate a new Random object, you can code this:

    Random random=new Random();
    

    In this way, we invoke the default constructor, i.e no argument constructor. But if you code this:

    Random random=new Random(47);
    

    we invoke a constructor which argument is 47.

    It is similar to C language; a seed will "randomly" create a number if you use the same seed and no matter when it will not change. But if you choose the first method, the number will modify once running!

    0 讨论(0)
  • 2021-01-22 04:35

    A pseudorandom number generator works by repeatedly generating a new number based on the one it has previously generated. That means that if you always have the same first "random" number, and you use the same pseudorandom number generator to generate the second, you'll always have the same second "random" number as well.

    The first Random constructor constructs a pseudorandom number generator with a nondeterminate seed (first number in the sequence), so you'll almost always end up with a different sequence of "random" numbers. The second Random constructor constructs a pseudorandom number generator with whatever seed you want, so if you give it the same seed, you'll always get the same sequence.

    Here's an example. If you create a Random like this:

    Random yourRandom = new Random();
    

    it will start off with some seed. That seed could be 42, 121, 3810, whatever. You can never be sure when you create it. All the random numbers it generates are based off of that seed, and so since it nearly always uses a different seed, you'll nearly always get different "random" numbers out of it.

    On the other hand, if you create a Random like this instead:

    Random yourOtherRandom = new Random(36);
    

    all the numbers yourOtherRandom generates will be calculated starting from 36. Since the first number (36) is the same, and the second number is calculated from the first, etc., everything yourOtherRandom generates will be the same every time you run your program.

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