Re: [PATCH] Add example to rand.3

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

 





On 12/28/22 22:03, Alejandro Colomar wrote:
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());

Oops, typo there

+    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: 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