From owner-freebsd-doc Thu Feb 20 12: 0:13 2003 Delivered-To: freebsd-doc@hub.freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 6D9C137B401 for ; Thu, 20 Feb 2003 12:00:10 -0800 (PST) Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id 694DD43F75 for ; Thu, 20 Feb 2003 12:00:07 -0800 (PST) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.12.6/8.12.6) with ESMTP id h1KK07NS005160 for ; Thu, 20 Feb 2003 12:00:07 -0800 (PST) (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.12.6/8.12.6/Submit) id h1KK07ZQ005159; Thu, 20 Feb 2003 12:00:07 -0800 (PST) Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id CC6E637B401 for ; Thu, 20 Feb 2003 11:51:57 -0800 (PST) Received: from darius.concentric.net (darius.concentric.net [207.155.198.79]) by mx1.FreeBSD.org (Postfix) with ESMTP id 89F7143FBF for ; Thu, 20 Feb 2003 11:51:56 -0800 (PST) (envelope-from pherman@frenchfries.net) Received: from cliff.concentric.net (cliff.concentric.net [206.173.118.90]) by darius.concentric.net [Concentric SMTP Routing 1.0] id h1KJpUw02798 for ; Thu, 20 Feb 2003 14:51:31 -0500 (EST) Received: from frenchfries.net (da001d0246.lax-ca.osd.concentric.net [66.239.48.246]) by cliff.concentric.net (8.9.1a) id OAA22575; Thu, 20 Feb 2003 14:51:27 -0500 (EST) Received: by frenchfries.net (Postfix, from userid 1000) id AD2484B0F; Thu, 20 Feb 2003 11:50:15 -0800 (PST) Message-Id: <20030220195015.AD2484B0F@frenchfries.net> Date: Thu, 20 Feb 2003 11:50:15 -0800 (PST) From: Paul Herman To: FreeBSD-gnats-submit@FreeBSD.org X-Send-Pr-Version: 3.113 Subject: docs/48493: missing range information in arc4random() manpage Sender: owner-freebsd-doc@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.org >Number: 48493 >Category: docs >Synopsis: missing range information in arc4random() manpage >Confidential: no >Severity: non-critical >Priority: medium >Responsible: freebsd-doc >State: open >Quarter: >Keywords: >Date-Required: >Class: doc-bug >Submitter-Id: current-users >Arrival-Date: Thu Feb 20 12:00:06 PST 2003 >Closed-Date: >Last-Modified: >Originator: Paul Herman >Release: FreeBSD 4.7-STABLE i386 >Organization: >Environment: System: FreeBSD mammoth.eat.frenchfries.net 4.7-STABLE FreeBSD 4.7-STABLE #0: Sat Jan 25 23:29:57 PST 2003 pherman@mammoth.eat.frenchfries.net:/u02/obj/u01/src/sys/mammoth i386 >Description: 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. >How-To-Repeat: man arc4random >Fix: 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 , >Release-Note: >Audit-Trail: >Unformatted: To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-doc" in the body of the message