Hi Branden, On 12/28/22 01:00, G. Branden Robinson wrote:
At 2022-12-28T00:33:13+0100, Alejandro Colomar wrote:The only problem with arc4random() is the lack of repeatability. When testing a program with random data, you'll need repeatable results. For that, rand(3) Just Works. When you want unpredictable results, you just seed it with some really random value, and you're fine. You need to be careful to not introduce bias, but there's nothing better in libc. It would be nice if libc provided a rand_uniform(3) variant of rand(3), BTW.Permit me to counsel against that last proposed name. In probability theory "uniform" is already widely and well understood to indicate the nature of the distribution. A "uniform" distribution is one in which each outcome is precisely equally likely.
That's exactly what I miss from the rand(3) interface; a variant that produces a value with uniform probability up to some maximum. There's the same for the arc4random() family of functions:
<https://www.gnu.org/software/libc/manual/html_mono/libc.html#index-arc4random_005funiform> For example I used something similar to this a long time ago: #include <stdbit.h> #include <stdint.h> #include <stdlib.h> uint32_t rand_uniform(uint32_t upper_bound) { uint32_t r; do { r = rand(); r %= stdc_bit_ceil(upper_bound); } while (r >= upper_bound); return r; }(I used C23 syntax for rounding up to a power of 2, to avoid some magic macro. That line is not necessary, but improves performance considerably for small upper bounds. I don't know if it's available already in GCC or Clang.)
That function is API-compatible with arc4random_uniform(), but it works with rand(3), so it can give repeatable results. If I didn't screw it, it gives uniform results.
rand_deterministic() would get the right idea across but is lengthy. Regards, Branden
Cheers, Alex
P.S. I asked Bertrand to tag groff 1.23.0.rc2 earlier today. Happy belated Boxing Day. ;-)
I saw :) BTW, the Ada thingy, if it's opinionated as you said, it will help considerably to make it easier to read ;)
-- <http://www.alejandro-colomar.es/>
Attachment:
OpenPGP_signature
Description: OpenPGP digital signature