Solved

Random Distribution Number Generator Algorithm

Posted on 2001-06-07
9
555 Views
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.
0
Comment
Question by:ronaldyoong
9 Comments
 

Expert Comment

by:nir_arad
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.

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

Accepted Solution

by:
deighton earned 50 total points
ID: 6163202
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
 
LVL 18

Expert Comment

by:deighton
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

0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 18

Expert Comment

by:deighton
ID: 6163264
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
 
LVL 2

Expert Comment

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




0
 
LVL 18

Expert Comment

by:deighton
ID: 6167760
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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 6170488
0
 
LVL 11

Expert Comment

by:griessh
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: http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

Please do not accept this comment as an answer!
======
Werner
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6828409
Per recommendation comment force/accepted by

Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

778 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