float random_value(int min, int max)
{
float temp = rand() / (float) RAND_MAX;
return min + temp *(max - min);
}
I have this function, but it is not working in cooja, as it always output the same numbers over and over again, and I don't have the time.h library to make srand()
. So, how should I print different random float values in cooja?
I should write a c function float random_value (float min, float max)
that could be used to implement virtual temperature sensor that could be configured to generate values between min and max value.
If you don't have access to
time.h
to seed the pseudo-random number generator, another way to use the currentpid
of the currently-running process.In fact, unique filenames are sometimes generated this way:
Here's a small example that prints the current
pid
and uses it as an argument tosrand()
:Running it once:
Running it again: