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

    randomNumber = rand()%10000;
    return randomNumber;

int main(){

    int randomNumber;
    cout << generateRandomNo(randomNumber);


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

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.


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

   for(int i=0;i<4;i++){
     for(int j=0;j<i;j++)             

Edited 1 Year Ago by phoenix254

This article has been dead for over six months. Start a new discussion instead.