More usage rand_s()

This commit is contained in:
AlexeyAB
2017-08-18 02:12:44 +03:00
parent d3577a565d
commit 4d2fefd75a
4 changed files with 42 additions and 20 deletions

View File

@ -39,17 +39,6 @@ char **get_random_paths_indexes(char **paths, int n, int m, int *indexes)
} }
*/ */
inline unsigned int random_gen()
{
unsigned int Num = 0;
#ifdef WIN32
rand_s(&Num);
#else
Num = rand();
#endif
return Num;
}
char **get_random_paths(char **paths, int n, int m) char **get_random_paths(char **paths, int n, int m)
{ {
char **random_paths = calloc(n, sizeof(char*)); char **random_paths = calloc(n, sizeof(char*));
@ -693,10 +682,10 @@ data load_data_detection(int n, char **paths, int m, int w, int h, int boxes, in
int dw = (ow*jitter); int dw = (ow*jitter);
int dh = (oh*jitter); int dh = (oh*jitter);
int pleft = rand_uniform(-dw, dw); int pleft = rand_uniform_strong(-dw, dw);
int pright = rand_uniform(-dw, dw); int pright = rand_uniform_strong(-dw, dw);
int ptop = rand_uniform(-dh, dh); int ptop = rand_uniform_strong(-dh, dh);
int pbot = rand_uniform(-dh, dh); int pbot = rand_uniform_strong(-dh, dh);
int swidth = ow - pleft - pright; int swidth = ow - pleft - pright;
int sheight = oh - ptop - pbot; int sheight = oh - ptop - pbot;
@ -727,7 +716,7 @@ data load_data_detection(int n, char **paths, int m, int w, int h, int boxes, in
void *load_thread(void *ptr) void *load_thread(void *ptr)
{ {
srand(time(0)); //srand(time(0));
//printf("Loading data: %d\n", random_gen()); //printf("Loading data: %d\n", random_gen());
load_args a = *(struct load_args*)ptr; load_args a = *(struct load_args*)ptr;
if(a.exposure == 0) a.exposure = 1; if(a.exposure == 0) a.exposure = 1;
@ -771,7 +760,7 @@ pthread_t load_data_in_thread(load_args args)
void *load_threads(void *ptr) void *load_threads(void *ptr)
{ {
srand(time(0)); //srand(time(0));
int i; int i;
load_args args = *(load_args *)ptr; load_args args = *(load_args *)ptr;
if (args.threads == 0) args.threads = 1; if (args.threads == 0) args.threads = 1;

View File

@ -1153,7 +1153,7 @@ void distort_image(image im, float hue, float sat, float val)
void random_distort_image(image im, float hue, float saturation, float exposure) void random_distort_image(image im, float hue, float saturation, float exposure)
{ {
float dhue = rand_uniform(-hue, hue); float dhue = rand_uniform_strong(-hue, hue);
float dsat = rand_scale(saturation); float dsat = rand_scale(saturation);
float dexp = rand_scale(exposure); float dexp = rand_scale(exposure);
distort_image(im, dhue, dsat, dexp); distort_image(im, dhue, dsat, dexp);

View File

@ -607,12 +607,13 @@ float rand_uniform(float min, float max)
max = swap; max = swap;
} }
return ((float)rand()/RAND_MAX * (max - min)) + min; return ((float)rand()/RAND_MAX * (max - min)) + min;
//return (random_float() * (max - min)) + min;
} }
float rand_scale(float s) float rand_scale(float s)
{ {
float scale = rand_uniform(1, s); float scale = rand_uniform_strong(1, s);
if(rand()%2) return scale; if(random_gen()%2) return scale;
return 1./scale; return 1./scale;
} }
@ -628,3 +629,32 @@ float **one_hot_encode(float *a, int n, int k)
return t; return t;
} }
unsigned int random_gen()
{
unsigned int rnd = 0;
#ifdef WIN32
rand_s(&rnd);
#else
rnd = rand();
#endif
return rnd;
}
float random_float()
{
#ifdef WIN32
return ((float)random_gen() / (float)UINT_MAX);
#else
return ((float)random_gen() / (float)RAND_MAX);
#endif
}
float rand_uniform_strong(float min, float max)
{
if (max < min) {
float swap = min;
min = max;
max = swap;
}
return (random_float() * (max - min)) + min;
}

View File

@ -63,6 +63,9 @@ int find_arg(int argc, char* argv[], char *arg);
char *find_char_arg(int argc, char **argv, char *arg, char *def); char *find_char_arg(int argc, char **argv, char *arg, char *def);
int sample_array(float *a, int n); int sample_array(float *a, int n);
void print_statistics(float *a, int n); void print_statistics(float *a, int n);
unsigned int random_gen();
float random_float();
float rand_uniform_strong(float min, float max);
#endif #endif