+ 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?

4th May 2017, 1:29 AM
jay
jay - avatar
36 Respostas
+ 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.
4th May 2017, 2:34 AM
Denis Felipe
Denis Felipe - avatar
+ 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.
4th May 2017, 1:47 AM
Karl T.
Karl T. - avatar
+ 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.
4th May 2017, 1:35 AM
Karl T.
Karl T. - avatar
+ 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
4th May 2017, 2:55 AM
Karl T.
Karl T. - avatar
+ 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
4th May 2017, 10:16 AM
Karl T.
Karl T. - avatar
+ 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
4th May 2017, 2:35 PM
Karl T.
Karl T. - avatar
+ 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.
5th May 2017, 12:15 AM
Karl T.
Karl T. - avatar
+ 7
Thanks heaps both to Denis and Helioform! Oh and Calvin!
4th May 2017, 10:30 AM
jay
jay - avatar
+ 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
4th May 2017, 1:39 AM
jay
jay - avatar
+ 6
Yes that article from Calvin is great...even has references from my Alma Mater, the IRO department of U of Montreal.
4th May 2017, 3:02 AM
Karl T.
Karl T. - avatar
+ 6
The article says that there is an AMD equivalent: RDSEED.
4th May 2017, 3:17 AM
Karl T.
Karl T. - avatar
+ 6
No you are right, I have misread. ;)
4th May 2017, 3:28 AM
Karl T.
Karl T. - avatar
+ 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.
4th May 2017, 9:49 AM
jay
jay - avatar
+ 6
I will give it a read in the morning! But thanks!
4th May 2017, 1:31 PM
jay
jay - avatar
+ 6
I have soooo much reading to do!
4th May 2017, 10:55 PM
jay
jay - avatar
+ 5
so to confirm. if one was to use rdrand properly you want an intel processor?
4th May 2017, 3:14 AM
jay
jay - avatar
+ 5
oh ok. i took the meaning that both amd and intel support rdrand. but only intel supports rdseed
4th May 2017, 3:23 AM
jay
jay - avatar
+ 5
:) you have just added another reason to the list "why I should really learn masm one day"
4th May 2017, 3:33 AM
jay
jay - avatar
+ 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.
4th May 2017, 3:39 AM
Karl T.
Karl T. - avatar
+ 5
knowing about rdrand is nice all the same
4th May 2017, 9:36 AM
jay
jay - avatar