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.
Printable View
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.
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
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)); }
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