The exercise was to generate 1000 random numbers between 0-9 and to print how many of each number were generated - and to repeat the task with ten different seeds. I used srand() with time() and yet I get the same distribution of numbers each time. Why is this?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int nums[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int i,j;
printf("1 2 3 4 5 6 7 8 9 10\n");
for (i = 0; i < 10; i++)
{
srand((unsigned int) time(0));
for (j = 0; j < 1000; j++)
nums[(rand() % 10)]++;
for (j = 0; j < 10; j++)
{
printf("%-6d", nums[j]);
nums[j] = 0;
}
printf("\n");
}
getch();
return 0;
}