how to get a random number between 0 and 1
C / C++ / MFC
4
Posts
4
Posters
0
Views
1
Watching
-
Did you try the rand()[^] function? It returns an int, but you can always divide the resulting int by pow(10, sizeof(int) + 1) to get a number between 0 and 1. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
std::rand() / (double)RAND_MAX gives a uniformly distributed random number in range [0...1]. Following function gives a uniformly distributed value in half-open range [lo…hi). template inline T random(T lo, T hi) { return (T)(lo + (__int64)std::rand() * (hi - lo) / (RAND_MAX + 1)); } Seppo -- modified at 9:09 Monday 10th October, 2005
-
How about 1 divid by the system generated random number? So, if the system generated number is 10 => 1/10 = 0.1 100 -> 1/100 = 0.01 & etc.