用指定種子生成隨機數

//Creates a Random instance with a seed of 12345.
Random random = new Random(12345L);

//Gets a ThreadLocalRandom instance
ThreadLocalRandom tlr = ThreadLocalRandom.current();

//Set the instance's seed.
tlr.setSeed(12345L);

使用相同的種子生成隨機數將每次返回相同的數字,因此如果你不想以重複數字結束,則為每個 Random 例項設定不同的種子是個好主意。

獲得每次通話不同的 Long 的好方法是 System.currentTimeMillis()

Random random = new Random(System.currentTimeMillis());
ThreadLocalRandom.current().setSeed(System.currentTimeMillis());