hey , I'm a beginner & need some help in C++ , I want to generate 4 unique numbers " any number isnt repeated within itself " I figured a way to generate 4 random numbers but they arent unique

srand((unsigned)time(0)); 
	      int number= (rand()%9999+1000);

, I also need to put these random numbers in an array so I could compare these random numbers to a punch of numbers that a user enters " I'm doing a number guessing game "

thx 4 advance 4 ur help & I really appreciate it :)

Recommended Answers

All 17 Replies

it's pretty easy to do.. just compare the number you with every occupied element in the array. if it is not in the array, add to array. else, try again.

std::set is a standard container that doesn't allow for duplicates. So, you can just do this:

#include <set>
#include <cstdlib>
#include <ctime>

int main() {
  srand((unsigned)time(0));
  std::set<int> numbers;
  while( numbers.size() < 4 )
    numbers.insert( rand() % 10 );
  return 0;
};

If its only 4 randomly generated numbers you need, then you can simply call rand() 4 times. It definitely should repeat just after 4 calls. Make sure you are using it correctly.

#include <iostream>
#include <ctime>
using namespace std;

int main(){
 srand( time(0) );
 for(int i = 0; i < 4; ++i){
   cout << rand()%9999+1000 << "\t";
 }
 return 0;
}

If its only 4 randomly generated numbers you need, then you can simply call rand() 4 times. It definitely should repeat just after 4 calls. Make sure you are using it correctly.

#include <iostream>
#include <ctime>
using namespace std;

int main(){
 srand( time(0) );
 for(int i = 0; i < 4; ++i){
   cout << rand()%9999+1000 << "\t";
 }
 return 0;
}

thx 4 ur help but I only need 1 number consisted of 4 unique digits , I need to know how to get only unique digits , thx :)

The first number will always be unique. Assign number to another value which should be your array, array[0] = number then the next random number you create, compare it to array[0]. If they are equal, make another random number until it doesn't equal array[0] then put it in array[1]. The third value you create you will need to compare to the first and second.

thx 4 ur help but I only need 1 number consisted of 4 unique digits , I need to know how to get only unique digits , thx :)

Oh wait so your problem is more like generating a 4-digit number in which each digit in that number is unique?

Oh wait so your problem is more like generating a 4-digit number in which each digit in that number is unique?

yupeee:D

The first number will always be unique. Assign number to another value which should be your array, array[0] = number then the next random number you create, compare it to array[0]. If they are equal, make another random number until it doesn't equal array[0] then put it in array[1]. The third value you create you will need to compare to the first and second.

a code 'd be much appreciated :)

yupeee:D

How do you think you should approach it then?

How do you think you should approach it then?

I was going to divide the number & generate each digit alone & make a loop , if a digit is repeated its regeneratedonce more till the digits are all unique & put them into an array & compare that array to the one that the user inputs " Im making a number guessing name ", I understand the concept but I'm 'ving trouble coding it, so I need all the help I can get :) thx :)

I'd go the other way around. Create a char array of size 5----to hold four visible char, each will be digits, and a terminating null char. The null char will allow you create a string,that can then be converted into an int using any of several protocols. To get the string of digits first create an array of four ints and fill it with four unique values each ranging from 0-9. To convert the elements of the int array to be char values you can take advantage of the ASCII chart. It lists the digits consecutively allowing you to add a numerical value, o-9, to the ASCII value of zero to get the ASCII value of the number you added to zero.

hello, i'm new member here and not so familiar with the forum here, there's something i wanna ask, i'm creating a game called deal or no deal using c++, may i know how to assign the random number to the boxes and output the boxes's number instead of the values assigned to it? and how to arrange the box separately? for i tried but it seems to stick together.
here's my code:

#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;

int main()
{
	double box[26];
	srand( time(NULL) );
	double amount_in_box[26]={.01,1,5,10,25,50,75,100,200,300,400,500,750,1000,5000,10000,25000,50000,75000,100000,200000,300000,400000,500000,750000,1000000};
	for(int i=0; i<26; i++)
	{
		int RandIndex=rand() % 26;
		box[i]=amount_in_box[RandIndex];
		cout<<box[i]<<endl;
	}		
	
}

this is the boxes:

#include<iostream>
using namespace std;

int main()
{
	
	int box[26]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,
	
				14,15,16,17,18,19,20,21,22,23,24,25,26};
	
	for( int i=0; i<26; i++)
	{
			cout<<" "<<"|"<<box[i]<<"|";
	}
}

the part for display box, when i use endl it cout in a straight line, when i use set.width(5), set. precision(2) and cout.setf(ios::fixed); before the cout<<" "<< "|"<<box<<"|";, it come out the same as without putting the set. things, so i'm quite stuck here...

thank you for helping me =)

Umm... not clear what u want to do... Do u want to print the corresponding amount u win when u pick a box ?
eg : if the random no randindex generates is 3, the amount shown is 5 . Is that what ur trying to do ?
Secondly, why r u using set width and set precision.. They dont show any difference here as they are meant to set the decimal point on a floating point number. In this case all ur numbers are whole numbers and there is no decimal.. hence no difference !

@HacruLeian

please read the rules before posting

You should have started your own thread instead of hijacking this thread
How would you feel if suddenly someone else posted a new question in your thread which made users ignore your own question

I Hope that a mod will move the previous posts....

um, i want to print the value inside the box i choose, but i don't have any idea to get the value, if i use assign then when output i think it will be the value inside the box instead of the box's number, as for the second part, i want to arrange it something like this:

1 2 3 4 5

6 7 8 9 10

but i got no idea how to do it.

sorry, i will post in a new thread

hmm... regarding arranging the boxes, do the following :

int j=0;
for( int i=0; i<26; i++)
	{
		cout<<" "<<"|"<<box[i]<<"|";
	              ++j;
              if(j%5==)
                 cout<<"\n";
}

So that dealt with, regarding storing the numbers in the box, what u are doing is correct, except giving a declaration inside the for loop, which leads to memory leak.
The following code should correct ur problem :

int RandIndex;
for(int i=0; i<26; i++)
	{
	    RandIndex=rand() % 26;
		box[i]=amount_in_box[RandIndex];
		cout<<box[i]<<endl;
	}
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.