0

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

}
3
Contributors
6
Replies
20
Views
2 Years
Discussion Span
Last Post by phoenix254
0

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

0

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

1

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.

0
 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);

Edited by phoenix254

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.