Understanding “randomness”

前端 未结 28 2618
轻奢々
轻奢々 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条回答
  •  猫巷女王i
    2020-11-22 16:01

    The answer would be it depends, hopefully the rand()*rand() would be more random than rand(), but as:

    • both answers depends on the bit size of your value
    • that in most of the cases you generate depending on a pseudo-random algorithm (which is mostly a number generator that depends on your computer clock, and not that much random).
    • make your code more readable (and not invoke some random voodoo god of random with this kind of mantra).

    Well, if you check any of these above I suggest you go for the simple "rand()". Because your code would be more readable (wouldn't ask yourself why you did write this, for ...well... more than 2 sec), easy to maintain (if you want to replace you rand function with a super_rand).

    If you want a better random, I would recommend you to stream it from any source that provide enough noise (radio static), and then a simple rand() should be enough.

提交回复
热议问题