Random long, can it be twice in a row the same number

后端 未结 4 1505
我在风中等你
我在风中等你 2021-02-13 10:42

I was wondering with the current java 1.7 implementation of the Random class, is it possible for the code below to generate two times the same random long?

Rando         


        
4条回答
  •  梦谈多话
    2021-02-13 11:08

    I don't think so. I believe that the generator uses the next number as the seed for the subsequent number. So, if you get a value once, and if it were to repeat, your number generator would be stuck in a loop.

    However, many applications are looking for a number in a certain range, which makes it possible to repeat since a subsequent number may have the same value modulo the range.

    EDIT: Since you included the source code of next you can see that if ever it returned the same number, it would always return the same number. Hence, you'd be stuck in a loop of one value.

提交回复
热议问题