我不得不猜测数字游戏的代码,但我不知道如何生成随机数。随机函数只产生负数。如何生成1000和-1000 C++之间的随机数?
的代码,我到目前为止有:
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main(){
srand(time(NULL));
int N=rand()%1000+-1000;
return 0;
}
使用['std :: uniform_int_distribution'](http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution)? –
当你从一个从0到999的随机数中减去1000时,负数就会出现=) – hidefromkgb