Why does rand() + rand() produce negative numbers?

后端 未结 9 1451
没有蜡笔的小新
没有蜡笔的小新 2020-12-22 18:30

I observed that rand() library function when it is called just once within a loop, it almost always produces positive numbers.

for (i = 0; i <         


        
9条回答
  •  既然无缘
    2020-12-22 19:03

    To avoid 0, try this:

    int rnumb = rand()%(INT_MAX-1)+1;
    

    You need to include limits.h.

提交回复
热议问题