Random Distribution Number Generator Algorithm

Posted on 2001-06-07
Last Modified: 2008-02-01
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.
Thanx for your help.
Question by:ronaldyoong
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 6162935
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.

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.
LVL 18

Accepted Solution

deighton earned 50 total points
ID: 6163202
Here is exponential for starters, include math.h

double exponential_distribution(double dlamda)
     double dprop;

          dprop = (double) rand() / (double) RAND_MAX;
     while (dprop == 0);

     return -log(dprop) / dlamda;

void main()

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

Expert Comment

ID: 6163206
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 18

Expert Comment

ID: 6163264
poisson distribution

long poisson_distribution(double lamda)
     double total=0;
     double dprop,dterm;
     long k=0;
     long c;
          dprop = (double) rand() / (double) RAND_MAX;
     } while (dprop == 1);

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

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

Expert Comment

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

LVL 18

Expert Comment

ID: 6167760

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.
LVL 11

Expert Comment

ID: 6811672
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.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is:

Please do not accept this comment as an answer!

Expert Comment

ID: 6828409
Per recommendation comment force/accepted by

Community Support Moderator
Experts Exchange

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question