From owner-freebsd-questions@FreeBSD.ORG Sun Jan 31 15:06:50 2010 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id EE196106566B for ; Sun, 31 Jan 2010 15:06:50 +0000 (UTC) (envelope-from lenthe@comcast.net) Received: from qmta08.westchester.pa.mail.comcast.net (qmta08.westchester.pa.mail.comcast.net [76.96.62.80]) by mx1.freebsd.org (Postfix) with ESMTP id 9AAE38FC14 for ; Sun, 31 Jan 2010 15:06:50 +0000 (UTC) Received: from omta14.westchester.pa.mail.comcast.net ([76.96.62.60]) by qmta08.westchester.pa.mail.comcast.net with comcast id cCXx1d0031HzFnQ58Etb1b; Sun, 31 Jan 2010 14:53:35 +0000 Received: from [192.168.1.112] ([71.224.157.103]) by omta14.westchester.pa.mail.comcast.net with comcast id cEta1d0082E88qF3aEtbAB; Sun, 31 Jan 2010 14:53:35 +0000 Message-ID: <4B65996D.2010908@comcast.net> Date: Sun, 31 Jan 2010 09:53:33 -0500 From: Jason Lenthe User-Agent: Thunderbird 2.0.0.23 (X11/20091128) MIME-Version: 1.0 To: Rolf Nielsen References: <4B64F938.5000605@lazlarlyricon.com> In-Reply-To: <4B64F938.5000605@lazlarlyricon.com> X-Enigmail-Version: 0.95.7 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Cc: FreeBSD Mailing List Subject: Re: Generating normally distributed random numbers. 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: Sun, 31 Jan 2010 15:06:51 -0000 > I am working on a project where I have the need to generate normally > distributed random positive integers, preferably unsigned 64 bit (or > even longer if possible) integers. More specifically, I will need the > ability to supply the expected value and the standard deviation for the > desired distribution, so a standard normal distribution will not do. > > Is there anyone out there who knows how to accomplish this? I have no > idea whatsoever, and for all I know there may already be a function that > does this in the math library. I'm quite accomplished when it comes to > math, but strangely I've never programmed computers for it. You won't any functions to do this in the standard C or C++ libraries (though you can readily build on rand()/rand_r()) Java has java.util.Random.nextGaussian(). Boost has normal_distribution.hpp. If you're going to do it yourself, usually normal deviates are generated using the polar method (http://en.wikipedia.org/wiki/Marsaglia_polar_method). If you really want to read up on this topic see Chapter 3 of Knuth's Art of of Computer Programming. The polar method is really designed for floating point numbers since you need log() but you could always round the values to the nearest integer. Getting getting normal deviates bigger than 64 bits could be a real challenge. A generator for a standard normal distribution is readily adapted to any normal distribution by multiplying by the standard deviation and adding the mean. Sincerely, Jason