Chaps I have written a lottery simulator ( I think ), but I can't help thinking that my logic is not correct on the conditional statement.
Basically int main() I use an bool array set to false and a random number gen which then sets the elements to true for the six numbers.
I then loop through the bool array and when true set that number to corrsponding element of the vector to store the numbers.
I pass this vector as const to a function that repeats the above but compares the vectors and if they are not the same the function runs again and checks const vect and so on and son on..
Can anyone put a learned eye over it for me please to check if my logic is correct - I'd really appreciate it!
The program is only to show my friends that they are bettor of buying a beer with the 2 quid lol !!!
#include <iostream>
#include <cstdlib>
#include <ctime>
#include<vector>
usingnamespace std;
//function to return true if lotto win
bool lottoWin( const vector<int>& myLine,unsignedlongint& numberOfDraws );
int main(void)
{
int size = 6; // for vectors to compare
bool lotterynumbers[59]; //array to store genertated numbers
vector<int> myLine(size);//vector to store 6 numbers
//integers to be used in for and while loops and amount of balls
int num,line,numberofballs = 59;
unsignedlongint numberOfDraws = 0;// pass by value to increment in fuction
srand( time(NULL) ); // randomise seed
line = 1; //number of balls in a line set to one and inc to <=6
//set all elements in lottrynumbers array to false
for ( int i = 1; i <= numberofballs; i++)
{
lotterynumbers[i] = false;
}
//generate 6 random numbers and set that element to true
while( line <= size)
{
num = 1 + (rand() % 59);
if ( !lotterynumbers[num] )
{
lotterynumbers[num] = true;
line++; //increment for stop condition of loop
}
}
int counter = 1;//used for indexing vector to store numbers
//loop through array and when it's true then put that value
//into vector myLine to pass to function later
for ( int i = 1; i <= numberofballs; i++)
{
if( lotterynumbers[i] )
{
myLine[counter] = i;
counter++;
}
}
cout << endl; // new line
cout << "Print vector";
for ( int i = 1; i <= size; i++ )
{
cout << myLine[i] << " ";
}
cout << endl;
if ( lottoWin( myLine, numberOfDraws ) )
{
cout << "You won after " << numberOfDraws << " tries" << endl;
}
elseif ( numberOfDraws == 25000000 )
{
cout << numberOfDraws << endl;
}
return 0; //finish succesfully
}
//function to gen new numbers like a draw and check against line
//generated previously
bool lottoWin ( const vector<int>& myLine, unsignedlongint& numberOfDraws )
{
bool lotterynumbers[59]; //array to store genertated numbers
int size = 6; // for vectors to compare
int draw = 1; //number of balls in a line set to one and inc to <=6
int numberOfBalls = 59;
int num = 0;
vector<int> lottoDraw(size);
bool returnValue = false;
numberOfDraws++;
// set all elements in lottrynumbers array to false
for ( int i = 1; i <= numberOfBalls; i++)
{
lotterynumbers[i] = false;
}
// generate 6 random numbers and set that element to true
while( draw <= size)
{
num = 1 + (rand() % 59);
if ( !lotterynumbers[num] )
{
lotterynumbers[num] = true;
draw++; //increment for stop condition of loop
}
}
int counter = 1;
//loop through array and when it's true print out that element
for ( int i = 1; i <= numberOfBalls; i++)
{
if( lotterynumbers[i] )
{
lottoDraw[counter] = i;
counter++;
}
}
//lottoDraw = myLine;
while (( myLine != lottoDraw ))
{
cout << numberOfDraws << endl;
numberOfDraws++;
//set all elements in lottrynumbers array to false
for ( int i = 1; i <= numberOfBalls; i++)
{
lotterynumbers[i] = false;
}
//generate 6 random numbers and set that element to true
while( draw <= size)
{
num = 1 + (rand() % 59);
if ( !lotterynumbers[num] )
{
lotterynumbers[num] = true;
draw++; //increment for stop condition of loop
}
}
int counter = 1;
//loop through array and when it's true print out that element
for ( int i = 1; i <= numberOfBalls; i++)
{
if( lotterynumbers[i] )
{
lottoDraw[counter] = i;
counter++;
}
}
}
returnValue = true;
return returnValue;
}