Reputation: 43
How does this expression work?
Num*rand() / RAND_MAX
If, for example, I replace the number with 7.7
, it gives me values from 0.
to 7.
(double
).
Upvotes: 2
Views: 2210
Reputation: 113
Tagging onto MrGreen's answer, I have found that when n is an integer, I will get an output of 0
when using double val = n * rand()/RAND_MAX;
. However, this can be addressed by a slight addition:
double val = (double) n * rand()/RAND_MAX;
Upvotes: 1