Understanding “randomness”

前端 未结 28 2459
轻奢々
轻奢々 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:14

    Oversimplification to illustrate a point.

    Assume your random function only outputs 0 or 1.

    random() is one of (0,1), but random()*random() is one of (0,0,0,1)

    You can clearly see that the chances to get a 0 in the second case are in no way equal to those to get a 1.


    When I first posted this answer I wanted to keep it as short as possible so that a person reading it will understand from a glance the difference between random() and random()*random(), but I can't keep myself from answering the original ad litteram question:

    Which is more random?

    Being that random(), random()*random(), random()+random(), (random()+1)/2 or any other combination that doesn't lead to a fixed result have the same source of entropy (or the same initial state in the case of pseudorandom generators), the answer would be that they are equally random (The difference is in their distribution). A perfect example we can look at is the game of Craps. The number you get would be random(1,6)+random(1,6) and we all know that getting 7 has the highest chance, but that doesn't mean the outcome of rolling two dice is more or less random than the outcome of rolling one.

提交回复
热议问题