C++中的随机数问题2

来源:百度知道 编辑:UC知道 时间:2024/07/08 02:39:03
我想要变量a是1~9的随机数,但是我的编程:#include "iostream.h"
#include "stdlib.h"
#include "time.h"
void main()
{
srand((unsigned int)time(NULL));
int i=rand()%10;
cout<<i<<endl;
}是0~9的,请问该怎么搞?

楼主可以试下x%9+1试试

#include "iostream.h"
#include "stdlib.h"
#include "time.h"
void main()
{
srand((unsigned int)time(NULL));
int i;
for(int p=0;p<100;p++)
{
i=rand()%10;
cout<<i<<" ";
}
cout<<endl;
}