# Thread: Get 5 random strings containing names

1. ## Get 5 random strings containing names

Code:
```I have really stuck in this laboratory, there i need to show on the program 5 random names, but it only pops up 1 name,
I Don't know what i m doing wrong here, if someone can help me out would be appreciated, sorry for my bad english ^^, the question is how to make the program get 5 random strings from it.

void SlumpaNamn(char cNamn2[5][10])
{
char cNamn[10][10];

strcpy(cNamn[0],"Benny");
strcpy(cNamn[1],"Glenn");
strcpy(cNamn[2],"Andreas");
strcpy(cNamn[3],"Daniel");
strcpy(cNamn[4],"Patrik");
strcpy(cNamn[5],"Anders");
strcpy(cNamn[6],"Gustav");
strcpy(cNamn[7],"Nisse");
strcpy(cNamn[8],"........e");
strcpy(cNamn[9],"Johnny");

int i,a;
char cTemp[10];
int iSeed = (int)time(NULL);

srand(iSeed);

for(i=0;i<5;i++)
{
strcpy(cTemp,cNamn[rand() % 9]);

for(a=0;a<i;a++)
{
if (strcmp(cNamn[i],cNamn[a]))
{
strcpy(cTemp,cNamn[rand() % 9]);
a=-1;
}
}
strcpy(cNamn2[i],cTemp);
printf("%s\n",cNamn2[i]);
}```

2. You should double-check your expectation about what strcmp does, with what strcmp actually does. (In particular, when is (strcmp(cNamn[i], cNamn[a])) going to be true?)

3. Why are you doing all that copying if all you need is to print 5 random names?

Just do:
Code:
```
for(i=0;i<5;i++)
printf("%s\n",cNamn[rand()%9]);```

4. Originally Posted by claudiu
Why are you doing all that copying if all you need is to print 5 random names?

Just do:
Code:
```
for(i=0;i<5;i++)
printf("%s\n",cNamn[rand()%9]);```
I believe OP is using "random" in the sense of "randomly chosen, but distinct".

5. Well that is a different definition of random every time then, since he is changing the range.

6. There seems to be a rash of code being posted with unindented code.

It's harder for you to work with, and more people will just ignore your post.