samedi 28 mars 2015

Assign unique random integers in an array (C)

im struggling with this so long, i can fill my array with random numbers but they are not unique. I can't spot the problem in my code :( Can you help me? Thanks



void fillArray(int *p)
{
int i;
for(i=0;i<N;i++)
p[i]=getUniqueNumber(p,i);
}

int getUniqueNumber(int *p, int i)
{
int x,j,found;
do
{
x=rand()%100000 + 1;
found=0;
j=0;
while(j<=i && found==0)
{
if(p[i]==x)
found=1;
else
j++;
}
} while(found==1);
return x;
}




Aucun commentaire:

Enregistrer un commentaire