samedi 10 février 2018

Create an array consisting of unique random number c++ [duplicate]

I have to fill the array of size n such that every element is unique and each element belongs to a range given by the user. A user will give two arrays l, and u consisting of lower and upper bounds. We have to fill the array such that element at pos p lies between l[pos] and u[pos] and the array should be unique at the end. The following code produces an array but there are chances that element is repeated.

cin>>n;
for(long long int i=0;i<n;i++) cin>>l[i]>>u[i];
for(long long int i=0;i<m;i++) cout<<l[i] + ( rand() % ( u[i] - l[i] + 1 ) )<<" ";

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire