Hi! How do I put the random number generated in an array so that I can make each number in it unique? Thanks! :)

#include <cstdlib> 
#include <ctime> 
#include <iostream>

using namespace std;

int generateRandomNo(int randomNumber){

    srand(time(0));
    randomNumber = rand()%10000;
    return randomNumber;
}

int main(){

    int randomNumber;
    cout << generateRandomNo(randomNumber);

}

I don't think you need to pass in any arguments to generateRandomNo

Hi Dave. How do I generate a random number composed of 4 unique numbers? Thanks!

Hi - give me an example of how this output would look?

Random number: 1234
It cannot be 1233 (since 3 appears twice in this number). Each number in the random number should be unique.

Ok I see.

Well first of all I am not a C++ programmer so cannot give you C++ code. I can give you pseudo code though.

Generate a random number between 1 and 9

Store this in a string

Generate another number between 1 and 9 and if this isn't already in the string, store it otherwise repeat. Use a loop for this.

Repeat the above x times or in this case 3 times in total. Wrap the above in a for statement.

Return the string cast to a number

Does that help? This is just one way of coding this off the top of my head.

 do{

   for(int i=0;i<4;i++){
     guess[i]=rand()% 6 + 1;      
   }

   mum=0;
   for(int i=0;i<4;i++){
     for(int j=0;j<i;j++)             
       if(guess[i]==guess[j])  
        mum=1;      
   }   
 }while(mum==1);
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.