c++语言中的随机数函数可以做加减法吗?

来源:百度知道 编辑:UC知道 时间:2024/09/12 17:28:35
#include<iostream>
#include"time.h"
using namespace std;
int main()
{int i, t,n=2,m;

for(i=0;i<=3;i++)
{ srand( (unsigned)time( NULL ) );
t=rand()%6;
m=5i+t;
cout<<m<<endl;
}
return 0;
}

一运行就报错,如果随机数出的数没有加减操作的话,就可以正常运行。高手们告诉我是怎么回事啊!

当然是可以做运算的

#include<iostream>
#include"time.h"
using namespace std;
int main()
{int i, t,n=2,m;
srand( (unsigned)time( NULL ) ); //随机数初始化放在这儿
for(i=0;i<=3;i++)
{
t=rand()%6;
//m=5i+t;//你这是啥?5i+t?5*i+t吧?
m=5*i+t;
cout<<m<<endl;
}
return 0;
}

#include<iostream>
#include"time.h"
using namespace std;
int main()
{int i, t,n=2,m;

for(i=0;i<=3;i++)
{ srand( (unsigned)time( NULL ) );
t=rand()%6;
m=5i+t; //m=5*i+t;
cout<<m<<endl;
}
return 0;
}

和随机数没有关系:
m=5i+t;
改为:
m=5*i+t;

t和m应该是float型
类型错误

5i

5*i

额,这个程序是来搞笑的吧。。。
开头连头文件(#include <stdlib.h> )都没加,你说C++语言会认你吗?