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

前端 未结 5 2087
我寻月下人不归
我寻月下人不归 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: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!

提交回复
热议问题