Bug 48493

Summary: missing range information in arc4random() manpage
Product: Documentation Reporter: Paul Herman <pherman>
Component: Books & ArticlesAssignee: Tom Rhodes <trhodes>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: Latest   
Hardware: Any   
OS: Any   

Description Paul Herman 2003-02-20 20:00:06 UTC
   The manpage for arc4random doesn't state the fact that the range is twice
   the traditional RAND_MAX.  This can lead to erroneous results if used
   like the traditional rand()/random() functions.  A short explanation
   of this fact, and a simple example should be added to the manpage.

   This was already discussed on -hackers.  See archive for details.

Fix: 

At this point, I had to manually edit the mdoc to:

-Remove the tables, we do not need them here.
-Replaced the #define line with:
.Dv #define Fn foo4random ( Fn arc4random Ft % ((unsigned)RAND_MAX + 1))
as it looks more correct.

Everything else was applied.--9BK228dwi6Un4Ea9mXzvG8u8Pqo3XucveTPK6ET17qiGjU19
Content-Type: text/plain; name="file.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="file.diff"

Index: arc4random.3
===================================================================
RCS file: /u02/ncvs/src/lib/libc/gen/arc4random.3,v
retrieving revision 1.8.2.4
diff -u -r1.8.2.4 arc4random.3
--- arc4random.3	14 Dec 2001 18:33:50 -0000	1.8.2.4
+++ arc4random.3	20 Feb 2003 19:32:58 -0000
@@ -57,7 +57,13 @@
 can be in about
 .if t 2\u\s71700\s10\d
 .if n (2**1700)
-states.
+states. The
+.Fn arc4random
+function returns pseudo-random numbers in the range of 0 to
+.if t 2\u\s731\s10\d\(mi1,
+.if n (2**31)\(mi1,
+and therefore has twice the range of
+.Dv RAND_MAX .
 .Pp
 The
 .Fn arc4random_stir
@@ -73,6 +79,15 @@
 since
 .Fn arc4random
 automatically initializes itself.
+.Sh EXAMPLES
+The following produces a drop-in replacement for the traditional
+.Fn rand
+and
+.Fn random
+functions using
+.Fn arc4random :
+.Bd -literal -offset indent
+#define foo4random()  (arc4random() % ((unsigned)RAND_MAX + 1))
 .Sh SEE ALSO
 .Xr rand 3 ,
 .Xr random 3 ,
How-To-Repeat:    man arc4random
Comment 1 Tom Rhodes freebsd_committer freebsd_triage 2003-05-01 20:33:52 UTC
State Changed
From-To: open->patched

Patch applied with some conditioning, see comments. 


Comment 2 Tom Rhodes freebsd_committer freebsd_triage 2003-05-01 20:33:52 UTC
Responsible Changed
From-To: freebsd-doc->trhodes

Over to me.
Comment 3 Tom Rhodes freebsd_committer freebsd_triage 2003-05-06 22:40:36 UTC
State Changed
From-To: patched->closed

MFC complete.  Thanks for the submission!