Date: Sun, 31 Jan 2010 09:53:33 -0500 From: Jason Lenthe <lenthe@comcast.net> To: Rolf Nielsen <listreader@lazlarlyricon.com> Cc: FreeBSD Mailing List <freebsd-questions@freebsd.org> Subject: Re: Generating normally distributed random numbers. Message-ID: <4B65996D.2010908@comcast.net> In-Reply-To: <4B64F938.5000605@lazlarlyricon.com> References: <4B64F938.5000605@lazlarlyricon.com>
next in thread | previous in thread | raw e-mail | index | archive | help
> 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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4B65996D.2010908>