Random Distribution Number Generator Algorithm

I need to build a random number generator. The number generated should follow some distribution like noemal distribution, Poisson distribution, exponential distribution and so on....
I'm using Visual C++ programming, but I will be appreciate if anybody can just let me know the algorithm.
I have gone through the exact generator I needed in Excel, but I can't get the source code.
Who is Participating?

x

Commented:
Here is exponential for starters, include math.h

double exponential_distribution(double dlamda)
{
double dprop;

do
{
dprop = (double) rand() / (double) RAND_MAX;
}
while (dprop == 0);

return -log(dprop) / dlamda;
}

void main()
{

printf("\nexponential lamda = 3 %lf",exponential_distribution(3));

}
0

Commented:
There are specific algorithm tips in
"Simulation Modeling and Analysis", 2nd Edition by Law and Kelton.
If you need something specific - I have the book at hand.

Example:
Bernoulli (p.472)
-----------------
1. Generate U~U(0,1)
2. If U <= p return X = 1. Otherwise return X = 0.

Uniform distribution can be obtained by the C library rand() function.
0

Commented:
include stdlib, stdio as well

and do a

// include time.h

srand( (unsigned)time( NULL ) );

at program start to seed the random number if you want different numbers every time you run the program

0

Commented:
poisson distribution

long poisson_distribution(double lamda)
{
double total=0;
double dprop,dterm;
long k=0;
long c;

do
{
dprop = (double) rand() / (double) RAND_MAX;
} while (dprop == 1);

do
{
dterm = (double) exp(-lamda);
for(c = 1;c <= k ; c++)
{
dterm *= lamda / c;
}

total += dterm;
k++;
} while(total < dprop);
k--;

return k;
}
0

Commented:
A 'Real' random number generator is impossible to build with a deterministic machine.

0

Commented:
yairy,

I agree that the rand function produces pseudo random numbers, a mathematical sequence that appears to be random, but obviously isn't as such random.  However they are adequate for most tasks,  and it is possible to replicate in c++ anything that excel can do.
0

Commented:
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days.
Unless there is objection or further activity,  I will suggest to accept "deighton" comment(s) as an answer.

The link to the Community Support area is: http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

======
Werner
0

Commented:
Per recommendation comment force/accepted by

Netminder
Community Support Moderator
Experts Exchange
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.