Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of posts: 16951

This Forum Only

Random number generation Posted by Thordaddy77 on 10 Oct 2012 at 9:59 PM
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?

Re: Random number generation Posted by Thordaddy77 on 11 Oct 2012 at 8:19 PM
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 <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
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
Re: Random number generation Posted by Thordaddy77 on 11 Oct 2012 at 8:21 PM
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 <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
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

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic