D编程错误

D编程错误,d,D,因为srand,我的程序无法运行。请帮忙。我用C++编写了程序,但是把它转换成D。我不理解SRAND如何在D.中工作。 main.d(18):错误:无法使用参数类型(long)调用函数core.stdc.stdlib.srand(uint seed) 导入标准stdio; 进口性传播疾病; 导入core.stdc.time; 导入core.stdc.stdlib; 国际刑事法院[8]; int a[8]; int b[8]; int c[8]; int countA,countB,countC;

因为srand,我的程序无法运行。请帮忙。我用C++编写了程序,但是把它转换成D。我不理解SRAND如何在D.</P>中工作。 main.d(18):错误:无法使用参数类型(long)调用函数core.stdc.stdlib.srand(uint seed)

导入标准stdio;
进口性传播疾病;
导入core.stdc.time;
导入core.stdc.stdlib;
国际刑事法院[8];
int a[8];
int b[8];
int c[8];
int countA,countB,countC;
int loserCount=0;
int p1,p2,p3;
void main(字符串[]args)
{
srand(时间(空));
int p1=rand()%7;
p1++;
int temp=rand()%7;
而(温度+1==p1)
{
temp=rand()%7;
}
p2=温度+1;
temp=rand()%7;
而(温度+1==p1 | |温度+1==p2)
{
temp=rand()%7;
}
p3=温度+1;
//书面形式(“罪犯:,p1,”,p2,”,p3);
对于(int i=0;i<8;i++)
{
如果(i==p1 | | i==p2 | | i==p3)
罪犯[i]=1;
其他的
罪犯[i]=0;
a[i]=0;
b[i]=0;
c[i]=0;
}
writefln(“\n开始游戏。有7个罪犯:1、2、3、4、5、6、7,其中3个是真正的罪犯。”);
while(true)
{
writefln(“3名随机罪犯:”);
int p1=rand()%7;
p1++;
int temp2=rand()%7;
而(temp2+1==p1)
{
temp2=rand()%7;
}
p2=temp2+1;
temp2=rand()%7;
而(temp2+1==p1 | | temp2+1==p2)
{
temp2=rand()%7;
}
p3=temp2+1;
writefln(“”,p1,p2,p3);
int tempCount=0;
如果(犯罪分子[p1]==1)
tempCount++;
if(罪犯[p2]==1)
tempCount++;
if(罪犯[p3]==1)
tempCount++;
writefln(“其中,tempCount是真正的罪犯”);
writefln(“有玩家想猜吗?(是/否)”);
查尔安斯;
while(true)
{
if(ans=='y'| | ans=='y')
{
如果(countA>-1)
{
writefln(“玩家1选择:”);
int tempA;
//cin>>坦帕;
if(罪犯[tempA]&&a[tempA]==0)
{
a[tempA]=1;
countA++;
}
else if(罪犯[tempA]==0)
{
countA=-1;
loserCount++;
}
}
如果(countB>-1)
{
writefln(“玩家2选择:”);
int tempB;
//cin>>tempB;
if(罪犯[tempB]&&b[tempB]==0)
{
a[tempB]=1;
countB++;
}
else if(罪犯[tempB]==0)
{
countB=-1;
loserCount++;
}
}
如果(countC>-1)
{
writefln(“玩家3选择:”);
int tempC;
//cin>>tempC;
if(罪犯[tempC]&&c[tempC]==0)
{
a[tempC]=1;
c++;
}
else if(罪犯[tempC]==0)
{
countC=-1;
loserCount++;
}
}
打破
}
else if(ans=='N'| | ans=='N')
{
打破
}
其他的
writefln(“输入错误!!”);
}
如果(countA=-1)
writefln(“玩家1输了”);
如果(countB==-1)
writefln(“玩家2输了”);
如果(countC==-1)
writefln(“玩家3输了”);
如果(loserCount==3)
{
writefln(“\n所有玩家都失去了\n\n游戏结束!”);
打破
}
如果(countA==3)
{
writefln(“玩家1赢!!”;
}
如果(countB==3)
{
writefln(“玩家2赢!!”);
}
如果(countC==3)
{
writefln(“玩家3赢!!”;
}
如果(countA==3 | | countB==3 | | countC==3)
{
writefln(“游戏结束!!”);
打破
}
}
}

当我试图在DMD上编译时,它给了我一条消息:“OPTLINK:Error 3:cannotcreatefile crimGame.exe”

错误消息的意思是在
srand(time(null))
srand需要一个uint(32位)整数作为参数,但
time
返回一个long(64位)。您需要将其转换为uint:

srand(cast(uint) time(null));
但是,在执行此操作之前,还应尝试在64位模式下编译。如果它在没有修改的情况下工作,请将强制转换更改为
cast(size\u t)
,因为它依赖于目标体系结构

另外,您不应该使用
srand
rand
而是应该使用
std.random
中的
uniform
,它是D中的随机函数。您甚至不需要设置种子,因为它会自动将一些不可预测的值(如时间、进程id、线程id等)作为种子


然后将
rand()%7
调用替换为
uniform(0,7)
(它生成一个0-6之间的随机值,因为它不包含7。它还将只生成整数,因为两个参数都是整数而不是浮点值)

错误消息意味着
srand(time(null))
srand需要一个uint(32位)整数作为参数,但
time
返回一个long(64位)。您需要将其转换为uint:

srand(cast(uint) time(null));
但是,在执行此操作之前,还应尝试在64位模式下编译。如果它在没有修改的情况下工作,请将强制转换更改为
cast(size\u t)
,因为它依赖于目标体系结构

另外,您不应该使用
srand
rand
而是应该使用
std.random
中的
uniform
,它是D中的随机函数。您甚至不需要设置种子,因为它会自动将一些不可预测的值(如时间、进程id、线程id等)作为种子

然后将
rand()%7
调用替换为
uniform(0,7)
(这将生成一个0-6之间的随机值,因为它不包括7)。