From 3c530a48c4cb1f5131a3bfab69dd481309548aa8 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Thu, 27 Jul 2017 17:14:55 -0700 Subject: [PATCH 1/2] get_random_num: Simplify random() is available everywhere except Windows and DOS. Also switch Windows to rand_s. --- src/Makefile | 2 ++ src/shared.c | 21 +++++---------------- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/src/Makefile b/src/Makefile index e936a399c..168feb17c 100644 --- a/src/Makefile +++ b/src/Makefile @@ -250,11 +250,13 @@ CFLAGS_CROSS_WIN32 += -I$(OPENCL_HEADERS_KHRONOS)/ CFLAGS_CROSS_WIN32 += -I$(WIN_ICONV_32)/include/ CFLAGS_CROSS_WIN32 += -m32 CFLAGS_CROSS_WIN32 += -DWITH_HWMON +CFLAGS_CROSS_WIN32 += -D_CRT_RAND_S CFLAGS_CROSS_WIN64 := $(CFLAGS) CFLAGS_CROSS_WIN64 += -I$(OPENCL_HEADERS_KHRONOS)/ CFLAGS_CROSS_WIN64 += -I$(WIN_ICONV_64)/include/ CFLAGS_CROSS_WIN64 += -m64 CFLAGS_CROSS_WIN64 += -DWITH_HWMON +CFLAGS_CROSS_WIN64 += -D_CRT_RAND_S LFLAGS_CROSS_LINUX32 := $(LFLAGS) LFLAGS_CROSS_LINUX32 += -lpthread diff --git a/src/shared.c b/src/shared.c index 83442586d..c49ee3ed8 100644 --- a/src/shared.c +++ b/src/shared.c @@ -379,27 +379,16 @@ u32 get_random_num (const u32 min, const u32 max) if (low == 0) return (0); - #if defined (__linux__) + #if defined (_WIN) - u32 data; + u32 r; + rand_s(&r); - FILE *fp = fopen ("/dev/urandom", "rb"); - - if (fp == NULL) return (0); - - const int nread = fread (&data, sizeof (u32), 1, fp); - - fclose (fp); - - if (nread != 1) return 0; - - u64 r = data % low; r += min; - - return (u32) r; + return ((r % (max - min)) + min); #else - return (((u32) rand () % (max - min)) + min); + return (((u32) random () % (max - min)) + min); #endif } From 973678241100219b7a4ec6287d57aedfe26b3182 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Sat, 29 Jul 2017 15:54:20 -0700 Subject: [PATCH 2/2] Switch back to rand(). rand_s is broken in AppVeyor. --- src/Makefile | 2 -- src/shared.c | 5 +---- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Makefile b/src/Makefile index 168feb17c..e936a399c 100644 --- a/src/Makefile +++ b/src/Makefile @@ -250,13 +250,11 @@ CFLAGS_CROSS_WIN32 += -I$(OPENCL_HEADERS_KHRONOS)/ CFLAGS_CROSS_WIN32 += -I$(WIN_ICONV_32)/include/ CFLAGS_CROSS_WIN32 += -m32 CFLAGS_CROSS_WIN32 += -DWITH_HWMON -CFLAGS_CROSS_WIN32 += -D_CRT_RAND_S CFLAGS_CROSS_WIN64 := $(CFLAGS) CFLAGS_CROSS_WIN64 += -I$(OPENCL_HEADERS_KHRONOS)/ CFLAGS_CROSS_WIN64 += -I$(WIN_ICONV_64)/include/ CFLAGS_CROSS_WIN64 += -m64 CFLAGS_CROSS_WIN64 += -DWITH_HWMON -CFLAGS_CROSS_WIN64 += -D_CRT_RAND_S LFLAGS_CROSS_LINUX32 := $(LFLAGS) LFLAGS_CROSS_LINUX32 += -lpthread diff --git a/src/shared.c b/src/shared.c index c49ee3ed8..e11f4df1d 100644 --- a/src/shared.c +++ b/src/shared.c @@ -381,10 +381,7 @@ u32 get_random_num (const u32 min, const u32 max) #if defined (_WIN) - u32 r; - rand_s(&r); - - return ((r % (max - min)) + min); + return (((u32) rand () % (max - min)) + min); #else