From: Alejandro Colomar <alx.manpages@gmail.com>
To: "G. Branden Robinson" <g.branden.robinson@gmail.com>
Cc: "Cristian Rodríguez" <crrodriguez@opensuse.org>,
"Jonny Grant" <jg@jguk.org>,
"Florian Weimer" <fweimer@redhat.com>,
"Michael Kerrisk" <mtk.manpages@gmail.com>,
linux-man <linux-man@vger.kernel.org>,
"GNU C Library" <libc-alpha@sourceware.org>
Subject: Re: [PATCH] Add example to rand.3
Date: Wed, 28 Dec 2022 01:41:12 +0100 [thread overview]
Message-ID: <8d7b59a0-c068-93ec-7c8b-4a12d9f412e9@gmail.com> (raw)
In-Reply-To: <20221228000010.iyrekqdj6fi5sf7d@illithid>
[-- Attachment #1.1: Type: text/plain, Size: 2348 bytes --]
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 #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2022-12-28 0:41 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <105835f5-359c-2646-f609-e73459ee2d3b@jguk.org>
2022-12-26 22:29 ` Alejandro Colomar
2022-12-27 13:07 ` Cristian Rodríguez
2022-12-27 23:33 ` Alejandro Colomar
2022-12-28 0:00 ` G. Branden Robinson
2022-12-28 0:41 ` Alejandro Colomar [this message]
2022-12-28 12:21 ` Cristian Rodríguez
2022-12-30 18:15 ` Joseph Myers
2022-12-30 18:20 ` Alejandro Colomar
2022-12-30 18:50 ` Joseph Myers
2022-12-30 18:58 ` Alejandro Colomar
2022-12-30 19:11 ` Cristian Rodríguez
2022-12-30 21:08 ` Joseph Myers
2022-12-30 21:15 ` Internal organization of "the implementation" (was: [PATCH] Add example to rand.3) Alejandro Colomar
2022-12-30 21:50 ` Joseph Myers
2022-12-27 21:37 ` [PATCH] Add example to rand.3 Jonny Grant
2022-12-27 23:11 ` Alejandro Colomar
2022-12-28 20:51 ` Jonny Grant
2022-12-28 20:56 ` Alejandro Colomar
2022-12-28 21:03 ` Alejandro Colomar
2022-12-28 21:04 ` Alejandro Colomar
2022-12-28 21:25 ` Jonny Grant
2022-12-28 21:32 ` Alejandro Colomar
2022-12-28 21:04 ` Cristian Rodríguez
2022-12-28 21:11 ` Alejandro Colomar
2022-12-28 21:19 ` Jonny Grant
2022-12-28 21:18 ` Alejandro Colomar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=8d7b59a0-c068-93ec-7c8b-4a12d9f412e9@gmail.com \
--to=alx.manpages@gmail.com \
--cc=crrodriguez@opensuse.org \
--cc=fweimer@redhat.com \
--cc=g.branden.robinson@gmail.com \
--cc=jg@jguk.org \
--cc=libc-alpha@sourceware.org \
--cc=linux-man@vger.kernel.org \
--cc=mtk.manpages@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).