11

i need a library with functions for generating random number, given average, standard deviation and using one of three distribution - exponential, normal or unified.

even one of the three would help. i'm looking for something like this - http://www.codeproject.com/KB/recipes/zigurat.aspx, but in c.

thanks

Delli22
  • 305
  • 2
  • 8

2 Answers2

15

May I recommend the GNU Scientific Library either for use or for inspiration? It has several Random Number Distributions and is designed to be used from C and C++.

Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
11

uniform:
Generate a random number in the range [0,1] with uniform distribution:

double X=((double)rand()/(double)RAND_MAX);

Exponentional
generating an exponentional random variable with parameter lambda:

-ln(U)/lambda (where U~Uniform[0,1]). 

normal:
the simplest way [though time consuming] is using the central limit theorem, [sum enough uniformly distributed numbers] but there are other methods in the wikipedia page such as the box muller transform that generates 2 independent random variables: X,Y~N(0,1)

X=sqrt(-2ln(U))*cos(2*pi*V)
Y=sqrt(-2ln(U))*sin(2*pi*V)
where U,V~UNIFORM[0,1]

transforming from X~N(0,1) to Z~N(m,s^2) is simple: Z = s*X + m

Though you CAN generate these random numbers, I stand by @Amigable Clark Kant suggestion to use an existing library.

Community
  • 1
  • 1
amit
  • 175,853
  • 27
  • 231
  • 333
  • If the platform supports C++11, then the new, powerful random number facility is available. It already supports 20 common distributions. See the [C++11 FAQ](http://www.stroustrup.com/C++11FAQ.html#std-random). – ManuelAtWork Jan 22 '16 at 06:12