Hi everybody!

I dont want to lie and say this isn't an assignment because it is, and I have really got stuck here.

The assignment was to create a lottery-program which randomly displays 10 ordinary numbers and 5 extra numbers, all in between the interval of 1-50. The problem is the last task in which the numbers must not repeat itself.

I have tried using array but cant get it to work. The array must save each character and loop over again to compare them for not being displayed twice.
, I just cant do this?!

Any answer is a good and helpfull answer!

Best regards
Adam

#include <iostream>	//programkod för in- och utmatning
#include <ctime>	
#include <cstdlib>


using namespace std;

int main()

{


srand(time(0));


for(int ordinary=0; ordinary<10; ordinary++)
{
cout<<rand()%50+1<<endl;
}


cout<<endl<<endl;


for(int extra=0; extra<5; extra++)

{
cout<<rand()%50+1<<endl;
}


return 0;

}

Dani AI

Generated

Sorting the chosen numbers before printing is the simplest fix for the “numbers must come out in order” requirement. Two clean, idiomatic approaches work well here and avoid duplicates:

  • Build a pool of 1..50, shuffle it, take the first 10 as ordinary and the next 5 as extra, then sort each group before printing. This is conceptually what suggested but using modern C++ tools gives better randomness and portability.
  • Or draw distinct numbers (insert into a container that enforces uniqueness) and then sort the final container before output. That’s similar to ’s duplicate-check idea—just be sure, as found and fixed, to reset any per-draw flag you use.

For C++11 and later prefer <random> and std::shuffle over rand()/random_shuffle. Example: build the 1..50 pool, shuffle with std::mt19937, take slices for ordinary/extra, then std::sort each slice before printing.

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

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

    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
    std::mt19937 rng(seed);
    std::shuffle(pool.begin(), pool.end(), rng);

    std::vector<int> ordinary(pool.begin(), pool.begin()+10);
    std::vector<int> extra(pool.begin()+10, pool.begin()+15);

    std::sort(ordinary.begin(), ordinary.end());
    std::sort(extra.begin(), extra.end());

    for (int n : ordinary) std::cout << n << ' ';
    std::cout << '\n';
    for (int n : extra) std::cout << n << ' ';
    std::cout << '\n';
}

Notes: avoid rand()%50 (modulo bias) for anything beyond simple exercises—use uniform_int_distribution if you must draw directly. random_shuffle was deprecated; prefer std::shuffle with a proper engine. See the std::shuffle documentation for details: .

Recommended Answers

All 6 Replies

I used vectors for this because I like them and they are easy to use. I think this is what you are looking for it generates the numbers and then stores them into the usedNumbers vector and then each time it makes a number it checks to see if it was used already.

You could make the same thing work using arrays but you would want to set each element of usedNumbers to zero and then instead of doing the for() loop to the usedNumbers.size() you would use 15.

Anyways here is the code I hope it helps and you learn from it.

#include <iostream>
#include <vector>
#include <time.h>

using namespace std;

int main()
{
	vector<int> usedNumbers, ordinary, extra;
	srand(time(NULL));
	
	//make ordinary
	for( int i = 0; i < 10; i++ )
	{
		int number;
		bool used = false;
		do
		{
			number = rand()%50+1;
			
			for( int c = 0; c < usedNumbers.size() && usedNumbers.size() != 0; c++ )
			{
				if( number == usedNumbers[c] )
				{
					used = true;
				}	
			}
		}while(used);
		
		usedNumbers.push_back(number);
		ordinary.push_back(number);
	}
	
	//make extra
	for( int i = 0; i < 5; i++ )
	{
		int number;
		bool used = false;
		do
		{
			number = rand()%50+1;
			
			for( int c = 0; c < usedNumbers.size(); c++ )
			{
				if( number == usedNumbers[c] )
				{
					used = true;
				}	
			}
		}while(used);
		
		usedNumbers.push_back(number);
		extra.push_back(number);
	}
	
	for( int i = 0; i < ordinary.size(); i++ )
	{
		cout << ordinary[i] << " ";
	}
	cout << endl;
	
	for( int i = 0; i < extra.size(); i++ )
	{
		cout << extra[i] << " ";
	}
	cout << endl;

     
	system("PAUSE");
	return 0;
}

Hi sfuo!

I really appreciate your answer and I am going to look this over as best as I can to understand it, really, thank you so much!

If it is not so much trouble would you like to show how to do it in array? I know I'm asking to much but that would really be great to know as well.

Thanks in advance and for your recent answer!

Best regards
Adam

or just use random_shuffle if u can.

#include <ctime>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    srand ( unsigned ( time (NULL) ) );
    vector<int> numbers(50);
    vector<int> ordinary(10);
    vector<int> extra(5);
    for (int i=0;i<50;++i)
        numbers[i]=i+1;
    
    //roll for ordinary
    random_shuffle(numbers.begin(),numbers.end());
    copy(numbers.begin(),numbers.begin()+10,ordinary.begin());

    //roll for extra
    random_shuffle(numbers.begin(),numbers.end());
    copy(numbers.begin(),numbers.begin()+5,extra.begin());

    for(int i=0;i<10;++i)
        cout<<ordinary[i]<<", ";
    cout<<endl;

    for(int i=0;i<5;++i)
        cout<<extra[i]<<", ";
    cout<<endl;


    return 0;
}

Since I made the above code kidna fast I noticed while making this one that it has a fatal flaw in it where it just keeps looping if the number was taken since I didn't reset "used" back to false in the loop.

Here is the same code fixed up and using arrays.

#include <iostream>
#include <vector>
#include <time.h>

using namespace std;

int main()
{
	bool used;
	int usedNumbers[15], ordinary[10], extra[5], number;
	memset(usedNumbers, 0, sizeof(usedNumbers));//sets all elements of usedNumbers to 0
	
	srand(time(NULL));
	
	//make ordinary
	for( int i = 0; i < 10; i++ )
	{
		do
		{
			used = false;
			number = rand()%50+1;
			
			for( int c = 0; c < 15 ; c++ ) //nothing changed aside from setting limit to 15 instead of size
			{
				if( number == usedNumbers[c] )
				{
					used = true;
				}
			}
		}while(used);
		
		usedNumbers[i] = number;
		ordinary[i] =  number;
	}
	
	//make extra
	for( int i = 0; i < 5; i++ )
	{
		do
		{
			used = false;
			number = rand()%50+1;
			
			for( int c = 0; c < 15; c++ ) //nothing changed aside from setting limit to 15 instead of size
			{
				if( number == usedNumbers[c] )
				{
					used = true;
				}	
			}
		}while(used);
		
		usedNumbers[i+10] = number; //add 10 because dont want to overwrite the numbers we got from the above loop
		extra[i] = number;
	}
	cout << "run 3" << endl;
	
	for( int i = 0; i < 10; i++ )
	{
		cout << ordinary[i] << " ";
	}
	cout << endl;
	
	for( int i = 0; i < 5; i++ )
	{
		cout << extra[i] << " ";
	}
	cout << endl;

     
	system("PAUSE");
	return 0;
}

This is really great, thank you very much both!

I am now going to print this out and work it through alongside my book.

Thank you, never thought this page would be so helpfull and nice, but this proved me wrong, really wrong, you are all the best!

Best regards
Adam

Hi again!

Have been looking at this code you gave me sfuo and it all worked out great. I only forgot that the random numbers must come out in order, which I have no clue how to do?

Really apreciate an answer from you!

Thanks in advance!

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.