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.

Dani AI

Generated

Quick diagnosis: the duplicate in 's run was simply the same variable being printed twice (as already noted). Several answers here go the “generate then check for duplicates” route (, and ). That works, but a cleaner, faster pattern is “sample without replacement” — build the pool 1..48, shuffle it once, then take the first 7 (and use the 8th as a bonus). This eliminates retry loops and accidental reprints.

#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <random>

int main() {
    std::vector<int> pool(48);
    std::iota(pool.begin(), pool.end(), 1);           // 1..48

    std::random_device rd;
    std::mt19937 gen(rd());
    std::shuffle(pool.begin(), pool.end(), gen);      // unique random order

    for (int i = 0; i < 7; ++i) std::cout << pool[i] << ' ';
    std::cout << '*' << pool[7] << '*';               // bonus ball
    return 0;
}

Notes and tips: compile with C++11 or later. If you want the main balls displayed in ascending order, sort the first seven before printing. If you must use C library rand(), follow the “check-each-new-number” approach (call your duplicate-check routine on every generated number) — but prefer <random> for better quality and less bias. If using C++17, std::sample is another concise way to draw k unique elements without manually shuffling.

Recommended Answers

All 14 Replies

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

That wont work, you fergot to add curly brackets for the if statement.

how can i edit my post?

You can only edit your post within 30 minutes of posting it, you will just have to repost it with the correct changes.

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 :)

yep.. try learning loops salman :P

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;
    }
    
}

I thought you needed to #include <cstdlib> in order to use the rand() function...?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.