random number from -9 to 9 in C++
just wondering, if I have the following code: int randomNum = rand() % 18 + (-9); will this create a random number from -9 to 9? No, it won't. You're looking for: int randomNum = rand() % 19 + (-9); There are 19 distinct integers between -9 and +9 (including both), but rand() % 18 only gives 18 possibilities. This is why you need to use rand() % 19 . Your code returns number between (0-9 and 17-9) = (-9 and 8). For your information rand() % N; returns number between 0 and N-1 :) The right code is rand() % 19 + (-9); Do not forget the new C++11 pseudo-random functionality , could be an option