I am currently trying to create a list of skills and each list of skills generates 3 to 10 skills to put in the list and then randomly generates the skills that it places in that list. But I don't want skills repeating,meaning if I have 8 skills going in a list I don't want to have 3 skill 7's...etc. Unfortunately Im stuck where to go from what I have here, currently I have a list of skills generating from 3 to 10 but I don't know not add duplicate numbers to the list if a generated skill is already in the list.

class skill
{
      public:
        skill(){}
        skill(int i){x=i;}
        
        ~skill(){}
        int getx(){return x;}
        bool operator == (skill s){return (x==s.getx());}
        
        void operator = (skill s){x=s.getx();}
      
        void display(){cout<<"a"<<x<<" ";}
        
        void generate1(list<skill>*S);
private:
        int x;

};


void skill::generate1(list<skill>*Slist)
{
skill * s1; 
srand (time (NULL));


int R,x;
R = 3+(rand()%7)+1;

for(int index=0; index<R; index++)
{        
        x =(rand()%10)+1;
        s1 = new skill(index);
        Slist->push_back(*s1);
}


}

Recommended Answers

All 2 Replies

in order to avoid repetition, you will have to push skills you have selected into an array, and compare the contents against future randomly generated skills. if skill has been used, try again.

Ok I attempted doing what you stated,still receiving duplicates with this thou

Edit *Never mind I saw and fixed my problem*

int i = 0;
for(int index=0; index<R; index++)
{       
        x =(rand()%10)+1;
        for(int b = 0;b<= index ;b++)
        {
         if(a[b] == x)
         {
          break;
         }
         if(x!=a[b])
         {
          s1 = new skill(x);
          Slist->push_back(*s1);
         }
        }
        a[i]=x;
        i++;
}
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.