即使在调用srand()时也没有获得不同的随机值 我刚刚开始学习C++,我尝试制作一个小程序,随机选择哪些游戏和哪些团队可以投注于体育赌博。我有一个比较两个随机数的函数,越大的值就是游戏的结果

即使在调用srand()时也没有获得不同的随机值 我刚刚开始学习C++,我尝试制作一个小程序,随机选择哪些游戏和哪些团队可以投注于体育赌博。我有一个比较两个随机数的函数,越大的值就是游戏的结果,c++,C++,程序编译并运行,但出于某种原因,它最终总是选择一个结果,即主结果。有人能检查一下我的代码,让我知道我做错了什么吗?我已经看了一段时间了,看不出问题所在。我认为这与双精度到整数的转换或其他有关 #include <iostream> #include <string> #include <vector> #include <algorithm> #include <cmath> #include <ctime> using

程序编译并运行,但出于某种原因,它最终总是选择一个结果,即主结果。有人能检查一下我的代码,让我知道我做错了什么吗?我已经看了一段时间了,看不出问题所在。我认为这与双精度到整数的转换或其他有关

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <ctime>

using namespace std;
inline void keep_window_open() { char ch; cin>>ch; }

int a;
int b;
int z;
int games_in_total (int, int);
double vector_home(int, int);
double vector_away(int, int);
string selection(double, double);

//takes user input with regard to how many games are playing and how many games you'd like to bet on

int main()
{ 

    cout << "How many games are there this week? " << endl;
    cin >> a;
    cout << "How many games do you want to bet on this week? " << endl;
    cin >> b;

    cout << " " << endl;

    //calls two functions in order to randomly pick which games to bet on and which team to choose within those games.

    z = 0;
    while (z < b){

    cout << "Pick game No." << games_in_total(a,b) << '\t' << "and choose" << selection((vector_home(a,b)), (vector_away(a,b))) << endl; 
    cout << " " << endl;
    ++z;

    }

    keep_window_open(); 
    return 0;

}

//randomly chooses games within the users input range
int games_in_total(int, int) {
    vector <int> games(0);
    srand (time(NULL));

    int i = 0;
    while(i<b){
    games.push_back(rand()% a+1);
    ++i;
        }

    return games[z];

}

//randomly assigns double values to the home team vector. Also adds 1.75 to the random number to give slight advantage to home teams.
double vector_home(int, int) {

    vector<double>home(0);
    srand (time(NULL));

    int i = 0;
    while(i<b){

    home.push_back((rand()% a+1) + 1.75);
    ++i;
    }

    return home[z];    
}

//randomly assigns double values to the away team vector
double vector_away(int, int) {
    vector<double>away(0);
    srand (time(NULL));

    int i = 0;
    while(i<b){

    away.push_back((rand()% a+1));
    ++i;
    }

    return away[z];
}

//compares the home team and away team vector values and assigns the larger values to the randomly chosen games to bet on.

string selection(double, double ){
    string pick_home;
    string pick_away;

    pick_home = " HOME.";
    pick_away = " AWAY.";

    if ((vector_home(a, b)) > (vector_away(a, b))){ 
         return pick_home;         
         }
    else 
         return pick_away;                  
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
内联void保持窗口打开({char ch;cin>>ch;}
INTA;
int b;
intz;
总游戏数(整数,整数);
双向量_home(int,int);
双向量距离(int,int);
字符串选择(双精度,双精度);
//根据用户输入,了解您正在玩多少个游戏以及您希望赌多少个游戏
int main()
{ 
库塔;
cout b;
库特
  • srand()
    初始化随机数生成器
  • time(NULL)
    返回1970年1月1日sinds的秒数
  • 由于在每次调用
    rand()
    之前调用
    srand(time(NULL))
    ,并且您的程序可能会在不到一秒钟的时间内执行,因此在99,99…%内,您将在每次调用
    rand()
    之前使用相同的种子初始化随机数生成器,从而得到
    rand()的结果
    将在整个程序运行期间保持不变
  • 此后,将
    1.75
    添加到起始值,其值将始终大于离开值
  • 你必须做的是:

    • 从代码中删除对
      srand()
      的所有现有调用
    • 只需在
      main()中调用一次
      srand()
  • srand()
    初始化随机数生成器
  • time(NULL)
    返回1970年1月1日sinds的秒数
  • 由于在每次调用
    rand()
    之前调用
    srand(time(NULL))
    ,并且您的程序可能会在不到一秒钟的时间内执行,因此在99,99…%内,您将在每次调用
    rand()
    之前使用相同的种子初始化随机数生成器,从而得到
    rand()的结果
    在整个程序运行过程中都将保持相同
  • 此后,将
    1.75
    添加到起始值,其值将始终大于离开值
  • 你必须做的是:

    • 从代码中删除对
      srand()
      的所有现有调用
    • 只需在
      main()中调用一次
      srand()

    games\u in_total()在循环中运行,这意味着srand(…)会被多次调用。您只需要srand(…)一次。但这可能不是您的问题。不要在不初始化变量的情况下声明变量。函数
    selection
    尤其可以(干净地!)写成两行,而不是代码中的9行(不包括空行)。太多的代码是可读性的敌人。
    games\u总计
    vector\u away
    vector\u home
    几乎是一样的。不要复制代码,只需编写一次代码并对其进行参数化。另外,为什么未使用参数?总之,您的代码的长度大约是所需长度的三倍。Vaibhav Desai,您是对的这是我的问题。康拉德·鲁道夫,谢谢你的建议,仔细想想,你是对的,这里有很多冗余。我会尽量让它变小,继续前进。games_in_total()在循环中运行,这意味着srand(…)被调用了很多次。你需要srand(…)只需一次。这可能不是你的问题。不要在不初始化变量的情况下声明变量。函数
    selection
    可以(干净地!)写成两行而不是9行,就像在你的代码中一样(甚至不算空行)。太多的代码是可读性的敌人。
    games\u总计
    vector\u away
    vector\u home
    几乎是一样的。不要复制代码,只需编写一次代码并对其进行参数化。另外,为什么未使用参数?总之,您的代码的长度大约是所需长度的三倍。Vaibhav Desai,您是对的这是我的问题。康拉德·鲁道夫,谢谢你的建议,再想一想,你是对的,这里有很多冗余。我会尽量缩小规模,继续前进。