Hey, my dad was checking the "lotto" results so that inspired me to make a little program of my own, i accomplished that just got a problem..

#include <iostream>

using namespace std;

int main()
{
    srand(time(NULL));
    
    unsigned int lotteryBall;
    
    for(int i = 0; i <=7; ++i)
    {
            lotteryBall = rand() % 48 + 1;
            
            cout << lotteryBall << " ";
            
            if(i == 7)
                 cout << "*" << lotteryBall << "*";
    }
    
    cin.get();
    
    return 0;
}

I compiled and ran the code and got : 13 19 20 9 31 12 37 46 *46*

But obviously you can't have 46 twice, so I'm not sure how I could change it.

And before you say "the numbers need to go in order from smallest to largest" I will add bubble sort after I have got the main problem solved.

Many thanks.

The reason you got 46 twice there isn't because the random number happends to be the same, its because you are printing the same variable twice without modifying it. If you want to make it so each number can only come up once, you have to make an array for the numbers to go into it and then check it to see if it has already been added.

I've modified the script a bit

#include <iostream>

using namespace std; 

int main()
{
    srand(time(NULL));
    
    unsigned int lotteryBall;
    
    for(int i = 0; i <=7; ++i)
    {
            lotteryBall = rand() % 48 + 1;
            
            cout << lotteryBall << " ";
            
            if(i == 7)
                 lotteryBall = rand() % 48 + 1;
                 cout << "*" << lotteryBall << "*";
    }
    
    cin.get();
    
    return 0;
}

just added 1 more line :P

Is this right?

#include <iostream>

using namespace std;

int main()
{
    srand(time(NULL));
    
    unsigned int lotteryBall[9];
    
    for(int i = 0; i <=7; ++i)
    {
            lotteryBall[i] = rand() % 48 + 1;
            
            cout << lotteryBall[i] << " ";
            
            if(i == 7)
{          do {
lotteryBall[i+1] = rand() % 48 + 1;} 
while ( 
(lotteryBall[i+1]==lotteryBall[0])|| 
(lotteryBall[i+1]==lotteryBall[1])|| 
(lotteryBall[i+1]==lotteryBall[2])||
(lotteryBall[i+1]==lotteryBall[3])||
(lotteryBall[i+1]==lotteryBall[4])|| 
(lotteryBall[i+1]==lotteryBall[5])||
(lotteryBall[i+1]==lotteryBall[6])||
(lotteryBall[i+1]==lotteryBall[7]));

                 cout << "*" << lotteryBall[i+1] << "*"; }
          }
    
    cin.get();
    
    return 0;
}

Probably much more efficient way that it can be done :)

would this be better or worse :(???

#include <iostream>

using namespace std;

int check (unsigned int checkvariable[]);

int main()
{
    srand(time(NULL));
    
    unsigned int lotteryBall[9];
    
    for(int i = 0; i <=7; ++i)
{
            lotteryBall[i] = rand() % 48 + 1;
            
            cout << lotteryBall[i] << " ";
            
            if(i == 7)
  {
        do {
            lotteryBall[i+1] = rand() % 48 + 1; 
            }while (check(lotteryBall));
  cout << "*" << lotteryBall[i+1] << "*"; 
  }
 
 }
    
    cin.get();
    
    return 0;
}

int check (  unsigned int checkvariable[])
{
    for(int a=0;a<=7;++a)
    {
            if (checkvariable[a]==checkvariable[8])
            {return true;
             break;}
             else if (a==7)
             return false;
    }
    
}

would this be better or worse :(???

#include <iostream>

using namespace std;

int check (unsigned int checkvariable[]);

int main()
{
    srand(time(NULL));
    
    unsigned int lotteryBall[9];
    
    for(int i = 0; i <=7; ++i)
{
            lotteryBall[i] = rand() % 48 + 1;
            
            cout << lotteryBall[i] << " ";
            
            if(i == 7)
  {
        do {
            lotteryBall[i+1] = rand() % 48 + 1; 
            }while (check(lotteryBall));
  cout << "*" << lotteryBall[i+1] << "*"; 
  }
 
 }
    
    cin.get();
    
    return 0;
}

int check (  unsigned int checkvariable[])
{
    for(int a=0;a<=7;++a)
    {
            if (checkvariable[a]==checkvariable[8])
            {return true;
             break;}
             else if (a==7)
             return false;
    }
    
}

You have some strange formatting here. If you line things up, it'll be easier to read. I changed lines 13 and 20 from 48 to 10 to drastically increase the odds of getting repeats and I got them. You want to call your check function each time you generate a new random number (not necessary the first time), not just for the last ball. Also, you have a function that returns an integer returning true and false. While that's not an error, you should probably have the function return a boolean value.

#include <iostream>
using namespace std;

int check (unsigned int checkvariable[]);

int main()
{
    srand(time(NULL));    
    unsigned int lotteryBall[9];
    
    for(int i = 0; i <=7; ++i)
    {
        lotteryBall[i] = rand() % 10 + 1;  
        cout << lotteryBall[i] << " ";
            
        if(i == 7)
        {
            do 
            {
                lotteryBall[i+1] = rand() % 10 + 1; 
            }
            while (check(lotteryBall));
            
            cout << "*" << lotteryBall[i+1] << "*"; 
        }
    }
    
    cin.get();    
    return 0;
}

int check (  unsigned int checkvariable[])
{
    for(int a=0;a<=7;++a)
    {
            if (checkvariable[a]==checkvariable[8])
            {
                return true;
                break;
            }
            else if (a==7)
                return false;
    }    
}

GOOD POINT, never noticed that.

so how about this

#include <iostream>
using namespace std;
bool check (unsigned int checkvariable[], int a);
int main()
{
    srand(time(NULL));
    unsigned int lotteryBall[9];
    for(int i = 0; i <=8; ++i)
{
            do 
            {
                lotteryBall[i] = rand() % 10 + 1;
            }
            while (check(lotteryBall, i));
            
            if(i == 8)
                  cout << "*" << lotteryBall[i] << "*";
            else 
                  cout << lotteryBall[i] << " ";
             
}    
    cin.get();  
    return 0;
}

bool check (unsigned int checkvariable[], int a)
{
    for(int b=0;b<=a;b++)
    {
            if (a==b)
            {
            return false;
            break;
            }
            if(checkvariable[b]==checkvariable[a])
            {
            return true;
            break;
            }      
    }
}

ONE QUESTION: I THOUGHT I READ THAT IF YOU WANTED TO USE THE SRAND() FUNCTION YOU MUST INCLUDE THE DIRECTIVE: ctime. In this case I did not and it still works??

wouldn't something like this be better? I mean if c++ gives the find function why not use it?

#include <iostream>
using namespace::std;

int main()
{
	srand(time(NULL));
	unsigned int lotteryBall[7];
	unsigned int tempBall;
	unsigned int* search;
	for(int i=0;i<7;i++)
	{
		tempBall=rand() % 10 + 1;
		search= find(lotteryBall, lotteryBall+7,tempBall);
		if(search==lotteryBall+7)
		{
		//number is not found
		lotteryBall[i]=tempBall;
		cout<< lotteryBall[i]<<' ';
		}
		else{i--;}
	}
	cin.get();
	return 0;
}

about ctime I am not sure but I do not include it and it works as well(at least with g++ on linux).

GOOD POINT, never noticed that.

so how about this

#include <iostream>
using namespace std;
bool check (unsigned int checkvariable[], int a);
int main()
{
    srand(time(NULL));
    unsigned int lotteryBall[9];
    for(int i = 0; i <=8; ++i)
{
            do 
            {
                lotteryBall[i] = rand() % 10 + 1;
            }
            while (check(lotteryBall, i));
            
            if(i == 8)
                  cout << "*" << lotteryBall[i] << "*";
            else 
                  cout << lotteryBall[i] << " ";
             
}    
    cin.get();  
    return 0;
}

bool check (unsigned int checkvariable[], int a)
{
    for(int b=0;b<=a;b++)
    {
            if (a==b)
            {
            return false;
            break;
            }
            if(checkvariable[b]==checkvariable[a])
            {
            return true;
            break;
            }      
    }
}

ONE QUESTION: I THOUGHT I READ THAT IF YOU WANTED TO USE THE SRAND() FUNCTION YOU MUST INCLUDE THE DIRECTIVE: ctime. In this case I did not and it still works??

Yeah, that program works well. srand is from cstdlib, not ctime. NULL is in ctime and cstdlib. I don't know why it works without cstdlib, but it does. Still, probably best to #include it anyway to be safe. If you don't #include iostream and add using namespace std , you seem to run into problems with this program regarding srand. Not sure why.

wouldn't something like this be better? I mean if c++ gives the find function why not use it?

#include <iostream>
using namespace::std;

int main()
{
	srand(time(NULL));
	unsigned int lotteryBall[7];
	unsigned int tempBall;
	unsigned int* search;
	for(int i=0;i<7;i++)
	{
		tempBall=rand() % 10 + 1;
		search= find(lotteryBall, lotteryBall+7,tempBall);
		if(search==lotteryBall+7)
		{
		//number is not found
		lotteryBall[i]=tempBall;
		cout<< lotteryBall[i]<<' ';
		}
		else{i--;}
	}
	cin.get();
	return 0;
}

about ctime I am not sure but I do not include it and it works as well(at least with g++ on linux).

This program gave good results too.

This article has been dead for over six months. Start a new discussion instead.