需要帮助足球模拟
Need help soccer simulation
我需要帮助来创造获胜条件并随机安排团队相互对抗…我随机选择球队来比赛,我总是让同样的球队打两场比赛或自己打一场比赛,我不知道该怎么办
#include <iostream>
#include <ctime>
#include <fstream>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <sstream>
using namespace std;
struct teams{//declaring a struct for the teams
string side;
int number;
int number1;
}teams1[16], points[16];
//void intro screen(){//function for the introduction screen
void fileData(){//function for reading the teams data file
ifstream input;
input.open("FootballTeam.txt",ios::in); //associate file
if(input.is_open()){//opening the file
for(int x=0; x<16; x++){//looping through the file
input>>teams1[x].side;//getting info from the file
cout<<teams1[x].side<<endl;//printing out the data from the file
}//end for
}//end if
}//end void
void play(){//function for playing the game
srand(time(NULL));
for(int x=0; x<=1; x++){//loop for random teams to play
for(int s=0; s<=7; s++){//loop for randoms goals value
x=rand() %16+1;//randomly selecting two teams
points[s].number=rand()%4+1;//randomly selecting goals
points[s].number1=rand()%7+3;//randomly selecting goals
cout<<teams1[x].side<<" :"<<points[s].number<<" vs "
<<teams1[s].side<<" :"<<points[s].number1<<endl<<endl;//printing out the teams and goals
//cout<<teams1<<" Won this match"<<endl;
}//end for
}//end for
}//end void
int main (){
cout<<"ROUND OF 16 Finalists!!!n"<<endl;
fileData();
cout<<"n";
system("PAUSE");
system("CLS");
play();
return 0;
}//end main
如果在使用rand()
之前不调用srand()
,则rand()
伪随机数生成器将使用其默认种子。为了防止rand()
在每次运行程序时使用默认种子,从而总是选择相同的团队对,您应该调用srand()
并传入time(NULL)
,我看到您已经这样做了。因为你的程序不会同时运行两次,rand()
将在每次运行时输出不同的数字。
但是,请注意您应该只调用一次srand()
。所以你需要在main()
中调用它,只要你的程序启动。现在每次调用play()
时都调用srand()
。每次调用play()
之间的时间间隔可能非常小。因此,由于时间差太小,rand()
最终每次都以几乎相同的数字播种。这有效地从伪随机数序列的同一点开始rand()
,这就是为什么你看到相同的团队相互比赛。
int main() {
srand(time(NULL));
// now you're free to use rand() for the rest of the program
// ...
}
查看srand()
的更多信息
相关文章:
- 如何使用Google Mock来模拟gettimeofday()
- 需要帮助设置在C++中使用的Potrace
- 在指针的帮助下,文本文件中单词的频率
- G锁定铸造到基础上会释放模拟行为
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 有什么好的方法可以让系统调用代理允许在单元测试中进行模拟
- 如何在Qbutton的帮助下更改Q对话框的宽度
- 需要帮助将结构数组传递给函数
- 落砂模拟碰撞检测C++和SFML
- 在gtest.中使用fff.h模拟系统API
- 谷歌模拟和覆盖关键字
- 用C#中的并集模拟C++嵌套结构
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- 在同一模拟中使用静脉和静脉_ inet内容时出现运行时错误
- 在模拟器中使用并集来模拟CPU寄存器有多合适
- 我写了一个C++程序来模拟Enigma机器.我没有得到输出
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- 有人可以帮助我处理正则表达式吗?
- 需要谷歌模拟帮助,
- 需要帮助足球模拟