How to produce non repeating numbers with srand

how do i make srand not repeat numbers?

here is my code

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
printf("This program asks the user to enter a number\n");
printf("Then it generates a set of six random numbers based\n");
printf("on the number given.\n\n");

int counter, i,;


srand(time( NULL ));

printf("Please enter how many sets you want generate: ");
scanf("%d", &counter);

for( i = 1; i <= counter; i++)
{
printf("%d\t", 1 + ( rand() % 53));
printf("%d\t", 1 + ( rand() % 53));
printf("%d\t", 1 + ( rand() % 53));
printf("%d\t", 1 + ( rand() % 53));
printf("%d\t", 1 + ( rand() % 53));
printf("%d\n", 1 + ( rand() % 53));
system("PAUSE");
}

return 0;
}
Last edited on
You can't force rand() to produce (or not produce) a certain number - it's random! If you srand() to a set number you'll get the same sequence of random numbers each run, but that's as far as it goes.

If you want to prevent your own program from repeating numbers, you'd need to keep track of all the previously generated numbers and make sure your next number isn't in that list.

Jim
Topic archived. No new replies allowed.