/ / Srand () - c ++ कहते हुए भी अलग-अलग यादृच्छिक मान प्राप्त नहीं करना

Srand () - c ++ को कॉल करते समय भी विभिन्न यादृच्छिक मान नहीं मिल रहे हैं

मैंने C ++ सीखना शुरू कर दिया है और मैंने एक बनाने की कोशिश की हैछोटे कार्यक्रम जो बेतरतीब ढंग से चयन करते हैं कि कौन से खेल और कौन से टीम खेल जुआ के लिए दांव लगाते हैं। मेरे पास एक फ़ंक्शन है जो दो यादृच्छिक संख्याओं की तुलना करता है, बड़ा मूल्य खेल का चुना हुआ परिणाम है।

कार्यक्रम संकलित करता है और चलाता है, लेकिन कुछ के लिएकारण यह हमेशा एक परिणाम, गृह परिणाम चुनने पर समाप्त होता है। क्या कोई मेरे कोड को देख सकता है और मुझे बता सकता है कि मैंने क्या किया "मैंने गलत किया है?" मैंने कुछ समय के लिए इसे देखा और "समस्या को देख नहीं सकता। मुझे लगता है कि यह युगल के साथ अंतर रूपांतरण या कुछ और करने के लिए है।

#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;
}

उत्तर:

उत्तर № 1 के लिए 4
  1. srand() यादृच्छिक संख्या जनरेटर को इनिशियलाइज़ करता है।
  2. time(NULL) १ सेकंड १ ९ 1st० के दशक की सेकंड की संख्या देता है।
  3. चूंकि आप फोन करते हैं srand(time(NULL)) इससे पहले कि कान में दर्द हो rand() और आपका कार्यक्रम संभवतः एक सेकंड से भी कम समय में, 99,99 में निष्पादित हो जाएगा ...% आप प्रत्येक कॉल से पहले एक ही बीज के साथ यादृच्छिक संख्या जनरेटर को शुरू करने का अंत करेंगे rand() और इस प्रकार का परिणाम है rand() आपके प्रोग्राम के पूरे रन के दौरान समान रहेगा।
  4. तब से आप जोड़ते हैं 1.75 घरेलू मूल्य के लिए, इसका मूल्य हमेशा दूर मूल्य से अधिक होगा।

तुम्हे जो करना है:

  • सभी मौजूदा कॉल को हटा दें srand() अपने कोड से
  • कॉल srand() बस एक बार अंदर main()