in your code you're looking for all numbers that CAN be a part of the factors sequence. Not the factors temselves.
This code will work:
for (int i = 2; i <= n; )
if (n % i == 0)
{
printf ("%d ", i);
n /= i;
i = 2;
}
else
i++;
Also, if you need to get random numbers any time you start the program, you should include <time.h> and add
srand(time(NULL));
in the beginning of the main() function;