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

后端 未结 4 1517
我在风中等你
我在风中等你 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:03

    It depends on which question you're asking.

    As others have said: The standard formulas for pseudo-random number generators fully explore their value space before repeating.

    However: In most applications we don't use the full range of the PRNG's output. We reduce it by dividing or truncating it down to a range that matches the problem we're trying to solve. And most of the ways we do so will, in fact, result in a series of numbers which can include immediate repeats.

    And that could be true even if you're simply using an integer random number when the underlying formula is using more bits for its calculations.

    So: Theoretically, if you're looking directly at a PRNG's output, the answer is "probably not". Practically, the answer is "don't count on it either way."

提交回复
热议问题