Wednesday, July 2, 2008

6/49 lottery number generator C++ code

When I visited Vancouver Canada in 2008, I played this 6/49 lottery. A friend of mine there asked me to write this code. It generates 6 random numbers range over [1,49].


/*
6/49 lottery number generator
*/

#include "iostream"
#include "vector"
#include
using namespace std;
int main ()
{
int iRndNum;
vector Numbers;
int i;

for(i=1; i <= 49; i++)
Numbers.push_back(i);


/* initialize random seed: */
srand (time(NULL) + rand());


cout << " The 6/49 numbers are" << endl;
for(i =1; i <= 6; i++){
/* generate a random number: */
iRndNum = rand() % (49 - i + 1) ;

cout << Numbers[iRndNum] << "\t";
Numbers.erase(Numbers.begin() + iRndNum, Numbers.begin() + iRndNum + 1);
}
cout << endl;

system("PAUSE");
return 0;
}

No comments: