From: Alejandro Colomar <alx.manpages@gmail.com>
To: Jonny Grant <jg@jguk.org>
Cc: Michael Kerrisk <mtk.manpages@gmail.com>,
linux-man <linux-man@vger.kernel.org>,
GNU C Library <libc-alpha@sourceware.org>,
Florian Weimer <fweimer@redhat.com>
Subject: Re: [PATCH] Add example to rand.3
Date: Wed, 28 Dec 2022 22:03:39 +0100 [thread overview]
Message-ID: <0c6b86cb-d471-2e35-89f3-840152c60aa2@gmail.com> (raw)
In-Reply-To: <b2ee2571-952e-4709-314d-ec4952a44ed8@gmail.com>
[-- Attachment #1.1: Type: text/plain, Size: 2045 bytes --]
Hi Jonny,
On 12/28/22 21:56, Alejandro Colomar wrote:
> Hi Jonny,
>
> On 12/28/22 21:51, Jonny Grant wrote:
>> You're completely right. It's a real issue if software starts multiple times
>> per second, or executes in less than one second and then runs again. Our
>> software always runs for at least minutes, maybe another code suggestion for a
>> seed would be good instead, like arc4random. I do like that rand() offers a
>> reproducible sequence, useful when in some other software we logged the seed
>> value used. random.4 - /dev/random would be a better seed than time(NULL) if
>> running the program multiple times per second. Anyway, rand() is only
>> pseudo-random, utilising /dev/random would be really much more random, and I
>> like that the seed is saved between reboots.
>
> Ahh, I didn't connect the dots the other day! We don't need to wait for glibc.
> libbsd already provides arc4random on GNU/Linux systems, so I can already
> recommend using arc4random to seed srand(3).
>
> I'll prepare a patch...
>
I will probably apply the following patch. Do you have any comments? Does it
provide the information you wanted to add?
Cheers,
Alex
diff --git a/man3/rand.3 b/man3/rand.3
index 572471749..350a875d8 100644
--- a/man3/rand.3
+++ b/man3/rand.3
@@ -190,6 +190,9 @@ .SH EXAMPLES
pseudo-random sequence produced by
.BR rand ()
when given a particular seed.
+When the seed is
+.IR \-1 ,
+the program uses a random seed.
.PP
.in +4n
.\" SRC BEGIN (rand.c)
@@ -211,7 +214,11 @@ .SH EXAMPLES
seed = atoi(argv[1]);
nloops = atoi(argv[2]);
- srand(seed);
+ if (seed == -1)
+ srand(src4random());
+ else
+ srand(seed);
+
for (unsigned int j = 0; j < nloops; j++) {
r = rand();
printf("%d\en", r);
@@ -223,5 +230,6 @@ .SH EXAMPLES
.\" SRC END
.in
.SH SEE ALSO
+.BR arc4random (3bsd),
.BR drand48 (3),
.BR random (3)
--
<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 21:03 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
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 [this message]
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=0c6b86cb-d471-2e35-89f3-840152c60aa2@gmail.com \
--to=alx.manpages@gmail.com \
--cc=fweimer@redhat.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).