From 9803fc41e8e6a820634f41d2196bdced7cbbc99f Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Wed, 17 Oct 2018 20:40:17 +0200 Subject: Use random() instead of rand() to increase portability Quoting glibc rand(3): The versions of rand() and srand() in the Linux C Library use the same random number generator as random(3) and srandom(3), so the lower-order bits should be as random as the higher-order bits. However, on older rand() implementations, and on current implementations on different systems, the lower-order bits are much less random than the higher-order bits. Do not use this function in applications intended to be portable when good randomness is needed. (Use random(3) instead.) --- src/collage.c | 4 ++-- src/gib_list.c | 2 +- src/keyevents.c | 2 +- src/main.c | 2 +- src/slideshow.c | 2 +- src/wallpaper.c | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/collage.c b/src/collage.c index 2a4d9f9..b616bef 100644 --- a/src/collage.c +++ b/src/collage.c @@ -145,8 +145,8 @@ void init_collage_mode(void) } /* pick random coords for thumbnail */ - xxx = ((w - www) * ((double) rand() / RAND_MAX)); - yyy = ((h - hhh) * ((double) rand() / RAND_MAX)); + xxx = ((w - www) * ((double) random() / RAND_MAX)); + yyy = ((h - hhh) * ((double) random() / RAND_MAX)); D(("image going on at x=%d, y=%d\n", xxx, yyy)); im_thumb = gib_imlib_create_cropped_scaled_image(im_temp, diff --git a/src/gib_list.c b/src/gib_list.c index 5384d98..a8ba1dd 100644 --- a/src/gib_list.c +++ b/src/gib_list.c @@ -362,7 +362,7 @@ gib_list_randomize(gib_list * list) } for (i = 0; i < len - 1; i++) { - r = i + rand() / (RAND_MAX / (len - i) + 1 ); + r = i + random() / (RAND_MAX / (len - i) + 1 ); t = farray[r]; farray[r] = farray[i]; farray[i] = t; diff --git a/src/keyevents.c b/src/keyevents.c index 689aebd..43bc82a 100644 --- a/src/keyevents.c +++ b/src/keyevents.c @@ -686,7 +686,7 @@ void feh_event_handle_generic(winwidget winwid, unsigned int state, KeySym keysy } else if (feh_is_kp(EVENT_jump_random, state, keysym, button)) { if (winwid->type == WIN_TYPE_THUMBNAIL) - feh_thumbnail_select_next(winwid, rand() % (filelist_len - 1)); + feh_thumbnail_select_next(winwid, random() % (filelist_len - 1)); else slideshow_change_image(winwid, SLIDE_RAND, 1); } diff --git a/src/main.c b/src/main.c index 1a76e2d..779e0bd 100644 --- a/src/main.c +++ b/src/main.c @@ -42,7 +42,7 @@ int main(int argc, char **argv) { atexit(feh_clean_exit); - srand(getpid() * time(NULL) % ((unsigned int) -1)); + srandom(getpid() * time(NULL) % ((unsigned int) -1)); setup_signal_handlers(); init_parse_options(argc, argv); diff --git a/src/slideshow.c b/src/slideshow.c index 3770677..b404318 100644 --- a/src/slideshow.c +++ b/src/slideshow.c @@ -258,7 +258,7 @@ void slideshow_change_image(winwidget winwid, int change, int render) case SLIDE_RAND: if (filelist_len > 1) { current_file = feh_list_jump(filelist, current_file, FORWARD, - (rand() % (filelist_len - 1)) + 1); + (random() % (filelist_len - 1)) + 1); change = SLIDE_NEXT; } break; diff --git a/src/wallpaper.c b/src/wallpaper.c index db14a8c..ef7ecca 100644 --- a/src/wallpaper.c +++ b/src/wallpaper.c @@ -252,7 +252,7 @@ void feh_wm_set_bg(char *fil, Imlib_Image im, int centered, int scaled, XGCValues gcval; GC gc; char bgname[20]; - int num = (int) rand(); + int num = (int) random(); char bgfil[4096]; char sendbuf[4096]; -- cgit v1.2.3