# Thread: yet another random num guestion

1. ## yet another random num guestion

Im trying to get a group 20 random numbers with no duplicates
I thought my code is right to do this, but i still am getting duplicates. What changes should I make? Any help is greatly appreciated.

Here my random number function:

void random (info data)
{
int count, counta, n, go, num[20];

for (count = 0; count < 20; count++) num[count] = 0;

srand ((unsigned)time(NULL));

for (count = 0; count < 20; count ++)
{
go = 0;

do
{
n = (rand() % 50);

for (counta = 0; counta < 20; counta++)
{
if (num[counta] == n) go = 0;
else go = 1;
}
}
while (go ==0);
num[count] = n;
}

out (num, data);
}

2. Firstly, if you're going to put code in your posts, please put them in [ code ][ /code ] tags (without the spaces).

Here's a possible solution:

Code:
```#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include <memory.h>

#define RAND_SIZE	100
#define NUMBERS		20

int main(void)
{
int flags[RAND_SIZE];
int numbers[NUMBERS];
int i;

memset(flags, 0, sizeof(flags));
memset(numbers, 0, sizeof(numbers));

srand(time(NULL));

for(i = 0; i < NUMBERS; i++)
{
int num = rand() % RAND_SIZE;

if(flags[num])
{
i--;
continue;
}

flags[num] = 1;
numbers[i] = num;
}

cout << "The random numbers are:\n";

for(i = 0; i < NUMBERS; i++)
{
cout << numbers[i] << endl;
}

return(0);
}```

3. thanks for the help and the tip about the [ code ] tags

4. no problem