commit 40c37b5c53a2635cfb1f4cb3a29c2a5f6648a0ea from: Sven M. Hallberg date: Wed Nov 22 23:37:41 2023 UTC use arc4random While crappy old rand(3) is fine for this program and uses arc4random(3) on OpenBSD anyway, the linker spews a warning about it and the noise during compilation is annoying me. FWIW, Linux finally got arc4random(3) with glibc >= 2.36 (2022) so it could actually be considered fairly portable now. However, for the time being, I'd like to still support older systems. NB, the closest "native" alternative, getentropy(3), appears in glibc >= 2.25 (2017), so even it is not exactly ancient, yet. Thur, instead of mucking around with wrappers, feature flags and conditional compilation, rand() is still it for portable. commit - 7100fec23ecb5b29fc48094dfc0c63b0a9f8f32f commit + 40c37b5c53a2635cfb1f4cb3a29c2a5f6648a0ea blob - 8384944b147e78e3f13a458ee7c35535b43f794b blob + fe0fd00b63ee0a618639f0f923f0c06ab1db8b3a --- teach.c +++ teach.c @@ -4,6 +4,7 @@ */ #include +#include #include #include #include @@ -325,7 +326,6 @@ teach(void) mistake = initmis; avgtime = initrsp; vartime = initvar; - srand(time(NULL)); puts("\n" " Type what you hear!\n" @@ -361,7 +361,7 @@ teach(void) l = 0; for (i = 0; i < nactive; i++) l += basemul * overall + failprob[i]; - x = l * rand() / RAND_MAX; + x = l * ((double)arc4random() / UINT32_MAX); assert(nactive > 1); for (i = 0; i < nactive - 1; i++) { x -= basemul * overall + failprob[i]; @@ -370,7 +370,7 @@ teach(void) } assert (i < nactive); /* avoid too many direct repeats */ - if (nactive > 2 && alphabet[i] == ch && rand() <= RAND_MAX/2) + if (nactive > 2 && alphabet[i] == ch && arc4random() % 2) continue; ch = alphabet[i];