about random numbers

Hi,

---
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/

( value % 30 + 1985 ) is in the range 1985 to 2014
---

i would like to ask how can we use decimal points in those numbers ?

for example the number 1988.71 ? How can we do this ?

Thanks!
closed account (o1vk4iN6)
if you want decimal points you can use floating point precision:

1
2
3

float v = (rand() % 3000) / 100.f + 1985.f;


the above code will only have precision up to the second decimal point though. I believe C++11 adds more random number generators including one for floats and doubles if you are looking for more accuracy.

http://en.cppreference.com/w/cpp/numeric/random
Instead of value%30, just use (value%3000)*100.0

EDIT : corrected /0.01 to /100.0
Last edited on
ok thanks!
Last edited on
Topic archived. No new replies allowed.