Re: [PATCH] Add example to rand.3

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




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@xxxxxxxx>
>>>>      * 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@xxxxxxxx>
>>>> Date: Mon, 26 Dec 2022 21:48:17 +0000
>>>> Subject: [PATCH] add rand.3 example
>>>>
>>>> Signed-off-by: Jonathan Grant <jg@xxxxxxxx>
>>>
>>> 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




[Index of Archives]     [Kernel Documentation]     [Netdev]     [Linux Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux