Trick the randomizer in C
c, random
Solution
To illustrate Sidoh's answer.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(int argc, char** argv)
{
int i;
srand ( (unsigned int)time(NULL) );
for (i = 0; i < 100; i++)
{
printf("%d ", 1 + (rand() % 10));
}
putchar('\n');
return 0;
}
This produced the following results for my one time seed using time( ).
7 10 2 4 4 4 2 1 7 7 10 4 3 10 2 9 6 9 2 9 7 10 4 1 1 8 2 4 8 1 2
4 2 3 9 5 8 1 7 4 9 8 10 1 8 1 1 5 1 4 5 7 3 9 10 3 6 1 9 3 4 10
8 5 2 7 2 2 9 10 5 9 8 4 1 7 7 2 3 7 5 8 6 10 8 5 4 3 7 2 8 2 1 7
7 5 5 10 6 5
Problem
I want to get random numbers between 1 to 10. It actually works, but when it's in a loop, I don't really get random numbers. ``` int randomNum; srand ( (unsigned int)time(NULL) ); randomNum = rand() % 10; ``` I've been spending hours here and in google looking for a solution, but it looks like no one really solved it (or maybe I didn't search good enough). The value we get from the randomizer depends on the seconds (not miliseconds or something else, like in other programming language) and that's why the numbers are not random. In addition, I don't want to download a package for C because I run my code in the university labs, and they won't allow it. Is there anyone with a creative solution for this problem? maybe some mathematic functions?