+ 10
rand() is bad?
so I was doing some research and came across several posts on the interwebs that say rand is bad and shouldn't be used. (we should use the <random> include instead) but was unable to find a clear reason why. Does anyone here know?
36 ответов
+ 1
The function rand is bad and will soon be deprecated. The reasons are plenty: the sequence can be predicted, the period of repetition is short, the modulo creates a bias towards smaller numbers and you can't select the distribution for the randomness. It is acceptable if you don't make anything serious, but it can become a problem in sensitive projects.
+ 12
Interesting. I don't think it's harmful it's just not complex enough for projects that require real randomness (if that even exists). They were supposed to base random functions in assembly based on the movement of subatomic particles. Intel was supposed to do that I don't know if they ever did in the end.
+ 10
There are different kinds of random number distributions, Normal, Binomial, Poisson etc. rand() is just the simplest I think it's a linear distribution.
+ 8
No prob. ☺ But I could also say that <random> is "bad" compared to an Intel assembly instruction that is compliant with security and cryptographic standards
https://en.m.wikipedia.org/wiki/RdRand
+ 8
Rdrand exploits the random thermal noise that is present in the CPU. You can learn more about hardware based RNGs here :
https://en.m.wikipedia.org/wiki/Hardware_random_number_generator
+ 8
@Denis, thanks that's good to know. You've convinced me to seriously consider <random> as a viable RNG.
And sorry but I won't take that challenge for 1xp, especially since you seem to be knowledgable about C++. xD
+ 8
@Denis, yes the different statistical distributions included in <random> is what I like the most. I'm going to use it in a game engine I'm developping.
+ 7
Thanks heaps both to Denis and Helioform! Oh and Calvin!
+ 6
ok. just asking cause there is one particular thing on msdn that says it(rand) is actually harmfull. i will link it here. about to listen to it myself. thought I would ask for the cliffnotes version first :)
https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
+ 6
Yes that article from Calvin is great...even has references from my Alma Mater, the IRO department of U of Montreal.
+ 6
The article says that there is an AMD equivalent: RDSEED.
+ 6
No you are right, I have misread. ;)
+ 6
i will be doing more research into this tomorrow. I hope to find out more as it has really got me hooked. If I ever do any proper encryption stuff I would probably use rdrand but I will be using <random> and mt for pretty much everything else from this point forward.
+ 6
I will give it a read in the morning! But thanks!
+ 6
I have soooo much reading to do!
+ 5
so to confirm. if one was to use rdrand properly you want an intel processor?
+ 5
oh ok. i took the meaning that both amd and intel support rdrand. but only intel supports rdseed
+ 5
:) you have just added another reason to the list "why I should really learn masm one day"
+ 5
Yeah, learning assembly is a great way to enhance your abilities as a programmer - even though most compilers optimize C++ better than what most can do with assembly.
+ 5
knowing about rdrand is nice all the same