Try this. It generates the number of random numbers you want, within the range of the numbers you want. If you want 10 random numbers, the range will be 0-9. If you want 100 random numbers, the range will be 0-99, etc.
Each number it gives, is unique. Edit: but see the next post for a better algorithm.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 10
int main() {
int i, n;
int numbers[SIZE];
time_t t;
printf("\n\n\n");
for(i=0;i<SIZE;i++) { //initalize numbers
numbers[i] = i;
}
srand((unsigned) time(&t));
printf("\nUnique random numbers:\n");
for(i=0;i<SIZE;i++) {
n=rand() % SIZE;
if(numbers[n]<0) //not unique, try again
--i;
else {
printf("%d\n", numbers[n]); //got one
numbers[n]=-1; //mark that index taken,
} //thus marking that number
}
printf("\n\n\t\t\t press enter when ready");
(void) getchar();
return 0;
}
Output:
Unique random numbers:
3
8
4
6
5
7
0
1
2
9
I just printed out the random numbers, but assigning them into another array, is nbd.