{
unsigned long n;
srand((unsigned)time(NULL));
for(int i = 0; i < 100; i++)
{
n = rand();
printf("d\n", n);
}
}
int rand (void);
Generate random number
Returns a pseudo-random integral number in the range between 0 and RAND_MAX.This number is generated by an algorithm that returns a sequence of apparently non-related numbers each time it is called. This algorithm uses a seed to generate the series, which should be initialized to some distinctive value using function srand.
A typical way to generate trivial pseudo-random numbers in a determined range using rand is to use the modulo of the returned value by the range span and add the initial value of the range:
1
2
3
v1 = rand() % 100; // v1 in the range 0 to 99
v2 = rand() % 100 + 1; // v2 in the range 1 to 100
v3 = rand() % 30 + 1985; // v3 in the range 1985-2014
Notice though that this modulo operation does not generate uniformly distributed random numbers in the span (since in most cases this operation makes lower numbers slightly more likely)