public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Jonny Grant <jg@jguk.org>
To: Alejandro Colomar <alx.manpages@gmail.com>,
	Florian Weimer <fweimer@redhat.com>
Cc: 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 20:51:13 +0000	[thread overview]
Message-ID: <da69ac41-1ea0-b852-4e9a-3d27a10f2bd7@jguk.org> (raw)
In-Reply-To: <3af45047-1d30-c4e8-cb73-a70eed6927fe@gmail.com>



On 27/12/2022 23:11, Alejandro Colomar wrote:
> Hi Jonny,
> 
> On 12/27/22 22:37, Jonny Grant wrote:
>> Hi Alex
>>
>> On 26/12/2022 22:29, Alejandro Colomar wrote:
>>> Hi Jonny,
>>>
>>> On 12/26/22 22:50, Jonny Grant wrote:
>>>> Hi Alejandro
>>>
>>> Please send also to my email.
>>>
>>>> Please find below a patch.
>>>>
>>>> 2022-12-26  Jonathan Grant <jg@jguk.org>
>>>>      * man3/rand.3: Add example to rand.3 seed with time(NULL)
>>>>
>>>>
>>>>   From 2d4501354ea6c465173fe6c089dfbcc80393a644 Mon Sep 17 00:00:00 2001
>>>> From: Jonathan Grant <jg@jguk.org>
>>>> Date: Mon, 26 Dec 2022 21:48:17 +0000
>>>> Subject: [PATCH] add rand.3 example
>>>>
>>>> Signed-off-by: Jonathan Grant <jg@jguk.org>
>>>
>>> time(NULL) is not too good.  If you call it several times per second, you'll find that it only changes the seed every second.  There are better ways to produce a good seed.
>>>
>>> However, I prefer suggesting arc4random() rather than workarounding rand(3) to get good results.
>>>
>>> Florian, did you already merge arc4random() to glibc?
>>
>> Hopefully arc4random will come soon. Maybe rand.3 could then be updated to SEE ALSO that. >
>> I would only mention to call srand once to seed, but you're right there are lots of other ways to get a seed.
>> Jonny
> 
> But consider the following case:
> 
> You're testing some program with random data in a loop.  And the program executes in less than a second.  Then srand(3) will be called several times per second with the same seed, and you'll get useless results.  I faced that exact situation a few years ago :)

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.

Cheers, Jonny


  reply	other threads:[~2022-12-28 20:51 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 [this message]
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=da69ac41-1ea0-b852-4e9a-3d27a10f2bd7@jguk.org \
    --to=jg@jguk.org \
    --cc=alx.manpages@gmail.com \
    --cc=fweimer@redhat.com \
    --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).