You may want to fill a certain array with an appropriate value. Or you may want to create a random value for your test data. Here are some tips that you can use in such cases.
Rand () / rand_r () is provided as a function to generate random numbers.
#include <stdlib.h>
int rand(void);
int rand_r(unsigned int *seedp);
However, the above function uses random numbers between 0 and RAND_MAX ** (* 1) **, so if you want to generate random numbers from 0 to UCHAR_MAX (255), you cannot use it as it is. ..
** (* 1) ** In my environment, it was defined in stdlib.h
as follows
/* The largest number rand will return (same as INT_MAX). */
#define RAND_MAX 2147483647
If you prepare the following wrapper function, you can specify the range of minimum / maximum value (0 to RAND_MAX).
#include <stdlib.h>
int getrand(unsigned int* seed, int min, int max)
{
return min + (int)(rand_r(seed) * (max - min + 1.0) / (1.0 + RAND_MAX));
}
DEMO
Below is a simple sample program. Please use it to check the operation.
https://github.com/ydah/specified_range_rand_r
Recommended Posts