This returns a pseudorandom integer up to Rand_max, usually at least 32767. Does anyone know what is the probability distribution of the numbers? Is there a random generator that returns 'standard' normal distribution with a mean of 0 and an sd of 1? Thanks Johan
Johan Steyn wrote:
This returns a pseudorandom integer up to Rand_max, usually at least 32767. Does anyone know what is the probability distribution of the numbers? Is there a random generator that returns 'standard' normal distribution with a mean of 0 and an sd of 1?
Typically, you'll get a reasonable approximation to a randomly chosen number between 0 and rand_max. For a 'standard' or Gaussian variate, try using the formula sqrt(-2*log((rand()+1)/(RAND_MAX+1)))*cos(2*PI*rand()/RAND_MAX) This is the Box-Müller method and is exact if rand()/RAND_MAX is a random number uniform on the range 0-1. The +1s are just to avoid arithmetic overflow and you ought to check what the standard library rand() actually computes. You can replace cos with sin if you like. If you want a more efficient and robust random number gernerator, try using the Gaussian generator in the GNU Scientific Library, which uses the polar-Marsaglia method. -- JDL
participants (2)
-
Johan Steyn
-
John Lamb