我是新手,请问为何我调用两次pokerdeal();时,结果都一样的?求大神相助!!
#include<iostream>
#include<ctime>
#include<iomanip>
using namespace std;
int pokerdeal(int n,int m){
srand(time(NULL));
n=rand()%4;
m=rand()%13;
{
switch(n){
case 0:cout<<"方块";break;
case 1:cout<<"梅花";break;
case 2:cout<<"红心";break;
default:cout<<"黑桃";
}
m=m+1;
if(m>=2&&m<=10) {cout<<m<<endl;}
else {
switch(m){
case 1: cout<<"A";break;
case 11: cout<<"J";break;
case 12: cout<<"Q";break;
default: cout<<"K";}
cout<<endl;
}
}
return 0;
}
int firstdeal(int a1,int a2,int a3,int a4){
pokerdeal(a1,a2);
pokerdeal(a3,a4);
}
int main(){
double poker[4][13];
int i,j,s,t;
for(i=0;i<=3;i++)
poker[i][0]=1;
for(i=0;i<=3;i++){
for(j=1;j>=1&&j<=9;j++)
poker[i][j]=j+1;
}
for(i=0;i<=3;i++){
for(j=10;j<=12;j++)
poker[i][j]=0.5;
}
/*for(i=0;i<=3;i++)
for(j=0;j<=12;j++)
cout<<poker[i][j]<<setw(4);*/
//pokerdeal(i,j);
firstdeal(i,j,s,t);
system("pause");
}
[解决办法]
srand(time(NULL)); 去掉
[解决办法]
srand(time(NULL)); 这句放到main里面就好
[解决办法]
time(NULL) 返回的是一个秒数,两次调用的时间差距如果小于1秒,此函数返回的是同样的数值
srand以它为种子,相同的种子会导致后面的rand方法得到相同的结果