Hi, I'm wondering how do I generate random numbers in decimals?
Meaning between 0-1.
Any ideas? rand() returns from the minimum of 1.
Hi, I'm wondering how do I generate random numbers in decimals?
Meaning between 0-1.
Any ideas? rand() returns from the minimum of 1.
Read Prelude's article on Using rand(), in particular about creating a uniform deviate.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thanks for the link, my mind must've slipped as this could also be done.
Code:float k = (rand()%10); k = k/10;
>float k = (rand()%10);
>k = k/10;
Apparently you didn't read the entire article. :P
My best code is written with the delete key.
I've always done it like so:
Code:float frand(int chop, int perk = 10) { return (float(rand() % chop) * pow(10.0, float(perk))) / pow(10.0, float(perk)); }
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I did read it quite a while ago, but I figured out that rand() was producing pretty much the same thing as her version of rand(). All I have to do is call srand() every 1k calls to rand().
Calling srand again after calling it the first time will NOT improve the distribution of your sequence - it will restart the sequence with a different seed, and depending on the seed value, it may give you the same sequence as last time or a different sequence. But it won't make the distribution any better.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.