How to generate random numbers from a normal disribution

I found a function but not sure whether it works.
1
2
3
4
5
6
7
double randgauss(double min, double max, double sigma, double centre)
{
	double random = (min + (max-min) * (double)rand()/RAND_MAX); //create random domain between [min,max]
	double tmp = (random-centre)/sigma; 
	double gauss= exp(-tmp*tmp/2); //gaussian formula
	return (gauss);
}


In matlab this can be done in one sentence but in C++ it becomes much more difficult.
Last edited on
It can be done in one sentence in C++ as well, except in C++ it is called "normal distribution", or, more exactly, std::normal_distribution

see for example
http://en.cppreference.com/w/cpp/numeric/random/normal_distribution
http://cplusplus.com/reference/std/random/normal_distribution/
http://msdn.microsoft.com/en-us/library/bb982827.aspx
Got it! Thanks so much for your help!
Last edited on
Topic archived. No new replies allowed.