I'm writing a simple number generator program and I'm trying to find an easier way to get a random number, store it then display it.

The catch is I don't want duplicated numbers (each need to be different).

here is what I have so far, it does work but I'll be working with more than just 3 numbers so this method would become very tedious to write up.

if(choice == 1)
		{
			system("cls");
			num1 = rand() % 9 + 1;
			num2 = rand() % 9 + 1;
			num3 = rand() % 9 + 1;
			
			if(num2 == num3 || num2 == num1)
			{
				num2 = rand() % 9 + 1;
			}

			if(num3 == num1 || num3 == num1)
			{
				num3 = rand() % 9 + 1;
			}

			if(num1 != num2 && num1 != num3)
			{
				numbers[0] = num1;
			}

			if(num2 != num3 && num2 != num1)
			{
				numbers[1] = num2;
			}

			if(num3 != num1 && num3 != num2)
			{
				numbers[2] = num3;
			}

Also FYI this isn't a homework assignment, it's just something to do in my sparetime at work.

Thanks.

Avoid using system("cls") (look here for why, the reasons are the same) :)

The system("cls") is just for me at the moment, I don't plan to keep it active, it's just so I can better see the results for the mean time.

There are a variety of ways to do this. These code snippets may be helpful.

http://www.daniweb.com/code/snippet1019.html
http://www.daniweb.com/code/snippet1034.html

In general, use a loop rather than picking the numbers all at once as you do. Start with an empty list/queue/vector/whatever, then generate a number. Check if it's in the list already. If it's already in the list, don't add it to the list. If not, add it. That prevents duplicates.

commented: Very nice work :) +3

> but I'll be working with more than just 3 numbers so this method would become very tedious to write up.
Think "pack of cards" and "shuffle"

The "pack" could be an array of 52, initialised with numbers 1 to 52
The "shuffle" swaps random pairs - the uniqueness of the numbers is preserved, but the order isn't.

you can use an array and a loop to generate random numbers as much as u want

int array[100]; // you can have it dynamically allocated.
srand(time(0)); // it will always generate a random number.

for (int i=0;i<100;i++)
{
array=rand%(number of your choice)
// you need to give some delay here to generate a random number
}

hope it will help u.

Vernon thanks a bunch for those links, it solved my issue very well. Took a little tweaking to get my program to not give me a stack corupted but once I found the magic number for my arrays it works great.

And Jawaad, once the program reaches the line to generate a random number it'll pause long enough to generate that number, it won't move on until that line is executed. Also an int array[100] isn't dynamic, thats a static array since you defined how large it can get. For a dynamic array I'd need to use a vector array, but since I know how many numbers I wish to display I don't need it to be dynamic.

> but I'll be working with more than just 3 numbers so this method would become very tedious to write up.
Think "pack of cards" and "shuffle"

The "pack" could be an array of 52, initialised with numbers 1 to 52
The "shuffle" swaps random pairs - the uniqueness of the numbers is preserved, but the order isn't.

One of the links VernonDozier posted actually worked with the cards idea, it would have worked fine for my program but like I said it's just something I'm doing to pass the time from boredom at work, his 2nd link gave me a very simple way to generate a random number while avoiding duplicates.

It's your call, but the technique suffers from taking an increasing amount of time to complete as most of the numbers have already been guessed.
You could be waiting a while for the last iteration to finish, when there's only one number left to guess.

commented: Good point. +21

It's your call, but the technique suffers from taking an increasing amount of time to complete as most of the numbers have already been guessed.
You could be waiting a while for the last iteration to finish, when there's only one number left to guess.

My program is just a simple lottery number generator. At most it'll select 6 numbers at random and give you the results. So the program isn't going to care what was previously guessed since none of the numbers are stored beyond them being displayed (after you press Enter they are released back into the mix). So speed really isn't a factor for it (at least not in it's current form).

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.