# Random Numbers with no Repitition

This is a discussion on Random Numbers with no Repitition within the C Programming forums, part of the General Programming Boards category; I'm trying to get a an array of 10 numbers filled with random numbers 0-9 with no repetition. I have ...

1. ## Random Numbers with no Repitition

I'm trying to get a an array of 10 numbers filled with random numbers 0-9 with no repetition. I have the following code:

Code:
```{
int i, j;
int used[10];
int temp;
int count = 0;
int check;

srand(time(NULL));
used[0] = rand() % 10;

for(i = 1; i < 10; i++)
{
check = 0;
do{
temp = rand() % 10;

for(j = 0; j < i; j++)
{
if(temp == used[j])
check = 1;
}
if(check != 1)
{
used[i] = temp;
check = 0;
}
}while(check != 0);

}```
I include stdio.h, stdlib.h, string.h, time.h.

When I run this, I get an infinite loop. When I manage to fix the infinite loop, I still get repetition.

Anyone see how I can fix these problems??

2. I assume the infinite loop problem is with the do while loop. But can't find it

3. "no repetition" - No.

But you can have an algorithm that does not repeat for a very long time (2^128-1 cycles) - See Xorshift - Wikipedia, the free encyclopedia

4. Nvm. Figured it out. All I had to do was put check = 0; into the do while loop

5. For some reason, I missed the rest of your post under your code explaining the problem - Sorry

Good to see you worked it out

6. Originally Posted by popnfresh12321
I'm trying to get a an array of 10 numbers filled with random numbers 0-9 with no repetition.
It is likely that the best way to do this is to pre-fill the array with the distinct integers 0 to 9, then shuffle the array, e.g., using Fisher-Yates/Knuth shuffle. This would be more efficient than the method you used of continually checking if the number generated has not been generated on a previous iteration.