# Thread: Random number generation without repeats

1. ## Random number generation without repeats

Hello friends,

Need help in generating random numbers from an array without repeats.Once a number is generated, the number has to be deleted from the array so that only the remaining numbers can be generated from the array the next time we call that function.And it should go on until all the numbers from the array are deleted.

The code i have developed is below

Code:
```#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>

int random ()
{
srand (time (NULL));

int picked[8], i;
for (i = 0; i < 9; i++)
picked[i] = 0;

int array[9];
int value;

for (i = 0; i < 9; i++)
{
value = rand () % 9;
if (picked[value])
i--;  // already picked.  for-loop increments, so decrement here
else
{
array[i] = value;
picked[value] = 1; // hasn't been picked yet.  Assign to array,
// flag as picked.
}
}

// display
for (i = 0; i < 9; i++)
printf("Values in the array are %d\n", array[i]);
getch();
return value;
}
int main()
{
int s;
s = random();

printf("value=%d", s);
getch();

}```
Hope replies asap from you friends.

2. Originally Posted by kishore84
Need help in generating random numbers from an array without repeats.Once a number is generated, the number has to be deleted from the array so that only the remaining numbers can be generated from the array the next time we call that function.And it should go on until all the numbers from the array are deleted.
Shuffle the array. Keep a pointer or index to the first element. Every time the function is called, pick the current element and increment the pointer or index (which could be an argument, or a static variable, depending on how you want to do it).