Baseball Random Generator Project

Hi, my teacher wants me to have a function that will handle the generation of the required random values. The code works but I don't know exactly what he is talking about. Somewhere along the lines of randNum=rand()%(high-low)+low. Any help is much appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <iomanip>
#include <math.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main()
{
    srand((time(NULL)));//Seeding at the start of the program

    int hit;
    int k;
    int SO=0;
    float SOE,SOT,SP; //Strike Out
    int GO=0;
    float GOE,GOT,GP; //Ground Out
    int FO=0;
    float FOE,FOT,FP; //Fly Out
    int FE=0;
    float FEE,FET,FPP; //Fielding Error
    int WA=0;
    float WAE,WAT,WP; //Walk
    int SI=0;
    float SIE,SIT,SPP; //Single
    int DO=0;
    float DOE,DOT,DP; //Double
    int TR=0;
    float TRE,TRT,TP; //Triple
    int HR=0;
    float HRE,HRT,HP; //Home Run

    for(k=0;k<5000;k++)
{

    hit=1+rand()%200;//Trying to get number 1 through 200 (instead of 100) to balance the decimal

if(hit<=44)
SO++;

else if(hit<=80)
GO++;

else if(hit<=120)
FO++;

else if(hit<=123)
FE++;

else if(hit<=146)
WA++;

else if(hit<=176)
SI++;

else if(hit<=191)
DO++;

else if(hit<=192)
TR++;

else if(hit<=200)
HR++;

}//end 'for' statement

//Experimental Percentage
    SOE=SO/50;
    GOE=GO/50;
    FOE=FO/50;
    FEE=FE/50;
    WAE=WA/50;
    SIE=SI/50;
    DOE=DO/50;
    TRE=TR/50;
    HRE=HR/50;

    //Theoretical Percentage
    SOT=22;
    GOT=18;
    FOT=20;
    FET=1.5;
    WAT=11.5;
    SIT=15;
    DOT=7.5;
    TRT=.5;
    HRT=4;

    //Error Percentage
    SP=((SO-1100)/1100.0)*100;//Times selected-Expected Times, divided by Expected Times, multiplied by 100
    GP=((GO-900)/900.0)*100;
    FP=((FO-1000)/1000)*100;
    FPP=((FE-75)/75.0)*100;
    WP=((WA-575)/575.0)*100;
    SPP=((SI-750)/750.0)*100;
    DP=((DO-375)/375.0)*100;
    TP=((TR-25)/25.0)*100;
    HP=((HR-200)/200.0)*100;

cout.setf(ios::showpoint|ios::fixed);
cout << setprecision(2);//No more than 2 numbers after decimal
cout << "                                         Batting Simulation" << endl;
cout << "                Number of          Experimental     Theoretical" << endl;
cout << "              Times Selected        percentage       percentage        %error" << endl;
cout << "Strike Out:" << setw(10) << SO << setw(23) << SOE << setw(17) << SOT << setw(16) << SP << endl;
cout << "Ground Out:" << setw(10) << GO << setw(23) << GOE << setw(17) << GOT << setw(16) << GP << endl;
cout << "Fly Out:" << setw(13) << FO << setw(23) << FOE << setw(17) << FOT << setw(16) << FP << endl;
cout << "Fielding Error:" << setw(6) << FE << setw(23) << FEE << setw(17) << FET << setw(16) << FPP << endl;
cout << "Walk:" << setw(16) << WA << setw(23) << WAE << setw(17) << WAT << setw(16) << WP << endl;
cout << "Single:" << setw(14) << SI << setw(23) << SIE << setw(17) << SIT << setw(16) << SPP << endl;
cout << "Double:" << setw(14) << DO << setw(23) << DOE << setw(17) << DOT << setw(16) << DP << endl;
cout << "Triple:" << setw(14) << TR << setw(23) << TRE << setw(17) << TRT << setw(16) << TP << endl;
cout << "Home Run:" << setw(12) << HR << setw(23) << HRE << setw(17) << HRT << setw(16) << HP << endl;


return 0;
}//end main
Last edited on
consider the <random> header (examples in ref section here) and <ctime> and <cstdlib> you are mixing C headers in.



hit=1+rand()%100;//Trying to get number 1 through 100
^^ success. this is a random number from 0-99 +1, which is 1-100.

If there is a problem, it isn't here.

however your srand (first statement in main) is rand not srand which is likely giving you the same numbers every time you run the code...
Last edited on
Thank you jonnin for helping. I did change the headers, but I had to keep the srand and had to change the generator parameters to balance the decimals in the calculations.
Topic archived. No new replies allowed.