From owner-freebsd-questions@FreeBSD.ORG Tue Sep 23 08:10:24 2008 Return-Path: Delivered-To: questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 16582106566B for ; Tue, 23 Sep 2008 08:10:24 +0000 (UTC) (envelope-from tedm@toybox.placo.com) Received: from mail.freebsd-corp-net-guide.com (mail.freebsd-corp-net-guide.com [65.75.192.90]) by mx1.freebsd.org (Postfix) with ESMTP id 9CE4C8FC1F for ; Tue, 23 Sep 2008 08:10:23 +0000 (UTC) (envelope-from tedm@toybox.placo.com) Received: from TEDSDSK (nat-rtr.freebsd-corp-net-guide.com [65.75.197.130]) by mail.freebsd-corp-net-guide.com (8.13.8/8.13.8) with SMTP id m8N7nJam087241; Tue, 23 Sep 2008 00:49:22 -0700 (PDT) (envelope-from tedm@toybox.placo.com) From: "Ted Mittelstaedt" To: "Robert Huff" , Date: Tue, 23 Sep 2008 00:51:02 -0700 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.6604 (9.0.2911.0) In-Reply-To: <18648.30321.369520.631459@jerusalem.litteratus.org> X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1933 Importance: Normal X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-3.0 (mail.freebsd-corp-net-guide.com [65.75.192.90]); Tue, 23 Sep 2008 00:49:22 -0700 (PDT) Cc: Subject: RE: using /dev/random X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 23 Sep 2008 08:10:24 -0000 > -----Original Message----- > From: owner-freebsd-questions@freebsd.org > [mailto:owner-freebsd-questions@freebsd.org]On Behalf Of Robert Huff > Sent: Monday, September 22, 2008 9:54 PM > To: questions@freebsd.org > Subject: using /dev/random > > > > What is the canonical way to get data from /dev/random? > Specifically: having opened the file, how do I read the stream? > I'm currently using > > > union { > float f; > char c[4]; > } foo; > > foo.f = 0.0; > > fscanf(rand_fp,"%4c",foo.c); > > > which doesn't seem to produce anywhere near "random bytes" as > promised by the man page. > > > Robert Huff > The canonical way is to use the functions random(), or srandom() or srandomdev() or arc4random() depending on what you need the random data for. /dev/random is really only useful for seeding these functions (some of them pull data from /dev/random internally) The thrust behind the FreeBSD /dev/random device is that we know that getting lots of real random data from /dev/random is difficult, however getting non-repeating seeds from /dev/random is easy. The device has thus been optimized for seed generation to feed these other functions. If you really want to roll-your-own and not use these functions then you could read blocks from /dev/random and run a Chi-square and Monte Carlo test on each block and discard the ones that don't pass. I've done my experimenting with the ENT program: http://www.fourmilab.ch/random/ ie: dd if=/dev/urandom bs=3000 count=100 of=random-sample ent random-sample Successive runs of that with different data sets and blocksizes clearly illustrates the generator can't pass Chi-square quite a lot of times. Ted