random number from -9 to 9 in C++

后端 未结 5 2099
伪装坚强ぢ
伪装坚强ぢ 2020-12-31 03:50

just wondering, if I have the following code:

int randomNum = rand() % 18 + (-9);

will this create a random number from -9 to 9?

5条回答
  •  天命终不由人
    2020-12-31 04:24

    Anytime you have doubts, you can run a loop that gets 100 million random numbers with your original algorithm, get the lowest and highest values and see what happens.

提交回复
热议问题