Sign in to follow this  

Implementing a Normal Distribution Probability

This topic is 3630 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, I'm looking for a way to implement a normal distribution probability in my code, and make it as efficient as possible. I've searched around a bit but found nothing, yet I'm sure someone has done it before. I'm not very good at maths, and I usually get confused with the terms and equations given in mathematical documents. However, I do understand the basic idea of the normal distribution, and believe it'll be good enough for me to use. Basically, I already have a Mean value, and a standard deviation, which I plan on inserting into an equation to make up the bell curve. Firstly: I'm not quite sure what that equation actually is because everywhere I search on the net there is a whole bunch of equations that don't make any sense to me. I was thinking of using this one:- http://en.wikipedia.org/wiki/Normal_distribution#Probability_density_function Secondly: I am looking to use this equation to generate a number that will adhere to the probability distribution given. So the number I generate will be more likely to be close to the mean value that I've given it. My initial Solution: First up I was just using a random number in a range of values, which is pretty bad because it uses a uniform distribution, which is not what I want, but will suffice for now. I have an idea to produce the probability equation, and then just randomly generate points on the graph until one falls under the curve. I'm a little unsure about this as it won't be very efficient. I'm looking at doing this without any libraries (except for using rand()). Can anyone please help me out with this, as I know it will give me good results for my project? Thanks in advance

Share this post


Link to post
Share on other sites
What language, compiler and libraries are you using? This kind of thing can be quite trivial with language or library support. For example, in C++ with boost, boost::random can be used to generate numbers in a normal distribution with one line of code.

Share this post


Link to post
Share on other sites
Oh sweet...thanks guys for the quick responses.

I'm using C++, but am looking to not use boost in case I want to improve it later.
It might work for now, but I won't want to use it in the end.

I did overlook the wikipedia section....and it does seem like the next step in doing what I'm after. Thanks for pointing it out.
But I'm still unsure what do c and d do? i.e how do I use them for my equation?

I kinda got lost in all of the equations that they give...so if anyone understands it a bit better could you please given an explanation?

Thanks heaps guys. I really appreciate it.

Share this post


Link to post
Share on other sites
If you have a hard time turning equations into code, you can use this implementation of the Box-Muller transform that I just wrote. If you don't use C++, it shouldn't be hard to rewrite this in another language.
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>

class StandardNormalSampleProvider {
double next_sample;
bool even;

double sample(){
if(!even){
even=true;
return next_sample;
}
even=false;
static const double pi=4.0*std::atan(1.0);
double U1=(.5+std::rand())/(1.0+RAND_MAX);
double U2=(.5+std::rand())/(1.0+RAND_MAX);
double m=std::sqrt(-2.0*std::log(U1));
double alpha=2.0*pi*U2;
double cos=std::cos(alpha);
double sin=std::sin(alpha);
next_sample = m*cos;
return m*sin;
}

public:
StandardNormalSampleProvider():even(true){
}

double operator()(double mean=0.0, double stddev=1.0){
return mean+stddev*sample();
}
};

int main(){
std::srand(std::time(0));
StandardNormalSampleProvider snsp;

for(int i=0;i<10;++i)
std::cout << snsp() << '\n';
}

Share this post


Link to post
Share on other sites
WOW!
Thanks heaps...

This looks great. Not quite sure how it works, but it's giving me some great results.
I'll read more into how it works, and try to understand it a bit more. Thank you so much. I really appreciate your help.

Thanks to everyone helping out (and your very quick replies).
Cheers

Share this post


Link to post
Share on other sites

This topic is 3630 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this