How to generate random number within x-y range in C where
X : [t , t+m] and Y : [r , r+m ].
That is , if x varies from t to t+n and y varies from r to r+n.
How to generate random number within x-y range in C where
X : [t , t+m] and Y : [r , r+m ].
That is , if x varies from t to t+n and y varies from r to r+n.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
double x = 1.0;
double y = 2.0;
srand(time(NULL));
// Guarenateed keep x1 between x and y.
double x1 = x + rand()*(y-x)/RAND_MAX;
printf("x1: %lf\n", x1);
return 0;
}
Basically you seed the generator with operation system ticks count, modulo your generated number with the upper range and add the lower range to the result.