Re: [PATCH] Add example to rand.3

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

 



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?

Cheers,

Alex

---
  man3/rand.3 | 14 ++++++++++++++
  1 file changed, 14 insertions(+)

diff --git a/man3/rand.3 b/man3/rand.3
index 572471749..c1542fb56 100644
--- a/man3/rand.3
+++ b/man3/rand.3
@@ -164,6 +164,20 @@ when good randomness is needed.
  .BR random (3)
  instead.)
  .SH EXAMPLES
+
+A possibly useful seed value would be by calling
+.BR rand ()
+with the result of
+
+.BR time ()
+as that varies with every call
+
+.EX
+srand((unsigned int)time(NULL));
+.EE
+.in
+.PP
+
  POSIX.1-2001 gives the following example of an implementation of
  .BR rand ()
  and

--
<http://www.alejandro-colomar.es/>

Attachment: OpenPGP_signature
Description: OpenPGP digital signature


[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