Understanding “randomness”

前端 未结 28 2485
轻奢々
轻奢々 2020-11-22 15:28

I can\'t get my head around this, which is more random?

rand()

OR:

rand() * rand()

I´m f

28条回答
  •  天涯浪人
    2020-11-22 16:09

    Consider you have a simple coin flip problem where even is considered heads and odd is considered tails. The logical implementation is:

    rand() mod 2
    

    Over a large enough distribution, the number of even numbers should equal the number of odd numbers.

    Now consider a slight tweak:

    rand() * rand() mod 2
    

    If one of the results is even, then the entire result should be even. Consider the 4 possible outcomes (even * even = even, even * odd = even, odd * even = even, odd * odd = odd). Now, over a large enough distribution, the answer should be even 75% of the time.

    I'd bet heads if I were you.

    This comment is really more of an explanation of why you shouldn't implement a custom random function based on your method than a discussion on the mathematical properties of randomness.

提交回复
热议问题