#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# Random number generation

Posts: 3Member
I am a total newbie, and I am pretty hung up on the implementation of a random number generation into a program I am supposed to write (C++). If I am given information about the probability of possible outcomes, how do I use a random number generator to predict the future events based on said probability figures?

The program is based on the next years baseball batting pcts and such. After rereading again and again I can only assume that I use each of the possibilities that add up to 100 percent, and use each of these separately ie. the probability for a single is 20 percent, so i use if(x >= 1 && x <=20) then blah blah blah.... Kind of stuck... Any way someone can push me in the right direction?

· ·

• Posts: 3Member
OK so I tried to work on it a little more, can someone give me an idea as to how correct my updated code is. i am trying to run a number generator 1000 times to get statistical probabilities for the upcoming season for a particular hitter. AM I WAY OFF??

#include
#include
#include
#include
using namespace std;

int main(){

double batAvg, slugPct;
int atBats = 0,
outs = 0,
walks = 0,
hits = 0,
singles = 0,
doubles = 0,
triples = 0,
homeRuns = 0,
randomNum = 1 + rand() % 100;
//srand(time(NULL)); // random number generator

//1-100
for(atBats=0; atBats <= 1000; atBats++)

hits = (singles + doubles + triples + homeRuns);
batAvg = hits / (atBats - walks);
slugPct = ((singles + (doubles * 2) + (triples * 3) + (homeRuns * 4)) /
(atBats - walks));

if(randomNum >= 1 && randomNum <=20)
singles += 1;

else if(randomNum > 20 && randomNum <= 35)
doubles += 1;

else if(randomNum > 35 && randomNum <= 44)
triples += 1;

else if(randomNum > 44 && randomNum <= 49)
homeRuns += 1;

else if(randomNum > 49 && randomNum <=65)
walks += 1;

else
outs += 1;

cout << "The projected number of singles is " << singles << endl;

cout << "The projected number of doubles is " <<
doubles << endl;

cout << "The projected number of triples is " <<
triples << endl;

cout << "The projected number of homeruns is " << homeRuns << endl;

cout << "The projected number of walks is " << walks << endl;

cout << "The projected number of hits is " << hits << endl;

cout << "The projected batting average for next year is " << batAvg << endl;

cout << "The projected slugging percentage for next year is " << slugPct << endl;

return 0;

}// end main
· ·
• Posts: 3Member
OK so I tried to work on it a little more, can someone give me an idea as to how correct my updated code is. i am trying to run a number generator 1000 times to get statistical probabilities for the upcoming season for a particular hitter. AM I WAY OFF??

#include
#include
#include
#include
using namespace std;

int main(){

double batAvg, slugPct;
int atBats = 0,
outs = 0,
walks = 0,
hits = 0,
singles = 0,
doubles = 0,
triples = 0,
homeRuns = 0,
randomNum = 1 + rand() % 100;
//srand(time(NULL)); // random number generator

//1-100
for(atBats=0; atBats <= 1000; atBats++)

hits = (singles + doubles + triples + homeRuns);
batAvg = hits / (atBats - walks);
slugPct = ((singles + (doubles * 2) + (triples * 3) + (homeRuns * 4)) /
(atBats - walks));

if(randomNum >= 1 && randomNum <=20)
singles += 1;

else if(randomNum > 20 && randomNum <= 35)
doubles += 1;

else if(randomNum > 35 && randomNum <= 44)
triples += 1;

else if(randomNum > 44 && randomNum <= 49)
homeRuns += 1;

else if(randomNum > 49 && randomNum <=65)
walks += 1;

else
outs += 1;

cout << "The projected number of singles is " << singles << endl;

cout << "The projected number of doubles is " <<
doubles << endl;

cout << "The projected number of triples is " <<
triples << endl;

cout << "The projected number of homeruns is " << homeRuns << endl;

cout << "The projected number of walks is " << walks << endl;

cout << "The projected number of hits is " << hits << endl;

cout << "The projected batting average for next year is " << batAvg << endl;

cout << "The projected slugging percentage for next year is " << slugPct << endl;

return 0;

}// end main
· ·