I am trying to generate the Gaussian Distribution using Method 2 and Method 3 described here:
http://c-faq.com/lib/gaussian.html
The problem is I am little confused as I have sigma and Mean and the 100 numbers with the range of 0 to 1 but in these methods it just returns the value for the interval of 0 to 1 and in these methods the sigma and mean value has not been used.
Can anyone help me how can I generate a Gaussian distribution using these methods?
the routines you linked to give random numbers (selected from a gaussian (normal) distribution) with mean 0 and standard deviation 1. that's the usual way routines like these work.
it's quite easy to change that to any other mean and sd - you just multiple by the sd and then add the mean.
so, for example, if x was generated by one of the routines above, then
y = 0.5 * x + 0.1
will have a standard deviation of 0.5 and a mean of 0.1.
so you don't need a separate routine for each combination of mean and sd. you just use the routines as given and then do the extra conversion.
Related
I was wondering how to use the %random% variable to pick a number within a range smaller then 0-30000 (I made a rough estimate). I read a couple of articles on this website and did not address my problem. In my program, I want to draw a random number from 0 to 5. Anyway one can do this?
Use the modulus function. It divides a number and returns the remainder. So divide by 6 and the range is 0 to 5 (6 units) if needing 1 to 6 add 1. See Set /?. The operators are C operators (https://learn.microsoft.com/en-us/cpp/c-language/c-operators).
This gives 1 to 6. Note the operator modulus % is escaped by another %.
Set /a num=%random% %% 6 + 1
echo %num%
The mod operator, %, is a relation on the set of integers that is injective (one-to-one) but not surjective (onto). It is therefore NOT a function proper because it is not bijective (both one-to-one AND onto (but we know what you mean)).
Care must be taken in the construction of the first half of your generator; the part that produces the integer to be modded. If you are modding at irregular clock intervals then the time of day down to the millisecond is just fine. But if you are modding from within a loop you must take care that you are not producing a subset of the full range that you wish to mod. That is: there are 1000 possible millisecond values in clock time. If your loop timing is regular to the extreme, you could be drawing small subset of integer values in millisecs on every call, and therefore producing the same modded values on every call, especially if you loop interval in msecs divides 1000 evenly.
You can use the rand() generator modulo 6 -- rand() % 6. This is what I do. You must however realize that rand() chooses without replacement integers in the range of 0 through 32767 using a recursive method (the next number produced depends entirely on the previous number drawn). Consider two numbers in the range, A and B. Initially, he probability that you draw A equals the probability that you will draw B equals 1/32768. Suppose on first draw you draw A, then the probability that you will draw A on the second draw is zero, and the probability that you will draw B is 1/32767.
One more thing: rand() is not a class and calls to it are GLOBALLY DEPENDENT within your program. So if you need to draw ranged random variables in different parts of your program the dependency described above with A and B still holds, even if you are calling from different classes.
Most languages provide a method of producing a REAL random number R, in the range 0.0 <= R < 1.0. These generators have no dependencies. In BASIC this method is rnd(), and you would code (rnd() * 1000) % 6, or some variation of that.
There are other homebrew methods of producing random variables. My fallback is the middle square method, which you can look-up anywhere.
Well, I have said a mouthfull and perhaps it seems like I am driving thumbtacks with a sledgehammer. But this information is always useful when using the built-in methods.
I am trying to generate a random probability with maximum of given probability. How could we generate a random real value between 0 and 0.5 in C?
You cannot generate a really random number in portable C99. But you could use some PRNG (and perhaps seed it with the current time).
And computers don't know about true real numbers. Only floating point. See http://floating-point-gui.de/
Actually I believe that the universe does not know about real numbers (think about some cardinality argument similar to Cantor's diagonal argument). But ask physicists or philosophers.
Some operating systems or implementations have PRNGs, and some systems (including) hardware have even genuine random generators.
Read about random(3) & drand48(3) if your system has it. If on Linux, read also random(4)
You might try
double my_random_number /* between 0 & 0.5 */ = drand48() * 0.5;
to generate an almost uniformly distributed random number >= 0 and < 0.5
See also C++11 standard header <random> if you accept to code in C++ ...
I have to write a C program to convert a uniform distribution of random numbers (say from 0 to 1) to a poisson distribution. Can anyone help?
Use GSL, the Gnu Scientific Library. There's a function called gsl_ran_poisson:
This function returns a random integer from the Poisson distribution with mean mu.
The probability distribution for Poisson variates is,
p(k) = {\mu^k \over k!} \exp(-\mu)
for k >= 0.
Otherwise, look at the code and copy the ideas.
I am assuming you want to write a C program that can sample a random number from the Poisson Distribution, given a random number in U(0,1).
Generally, this is done by taking the inverse CDF of the number from U(0,1). For discrete distributions like Poisson, one first transforms it to a continuous distribution by assuming that the CDF function is smooth between the integer points, and then we apply appropriate approximations (floor function).
The book Numerical Recipes in C++ (3rd Ed) has the complete explanation and C++ code as well. sec 7.3.12, page 372.
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
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++.
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.
Hey guys, I'm trying to compute the cumulative distribution function of the standard normal distribution for a formula in C using the GSL (Gnu Statistics Library)
I've installed and included gsl but am having trouble understanding how to use it.
I think the function I need is:
double gsl_ran_lognormal (const gsl_rng * r, double zeta, double sigma)
The formula I have only has one number that I would pass into a cdf function so I'm not quite sure what to do here. (This is probably because of my crappy understanding of statistics)
I would appreciate it anyone could lend me a hand on how to get the cdf using gsl with one input variable.
Documentation only says:
This function returns a random variate from the lognormal distribution. The distribution function is,
p(x) dx = {1 \over x \sqrt{2 \pi \sigma^2} } \exp(-(\ln(x) - \zeta)^2/2 \sigma^2) dx
for x > 0.
Basically, could someone explain what gsl_rng, zeta, and sigma should be?
EDIT: Ok, I think that zeta should be 0 (mu) and sigma should be 1 (std dev) to make it normal? Is that right? What is gsl_rng?
gsl_rng is a pointer to an initialized (and possible custom seeded) random number generator.
See for example http://www.csse.uwa.edu.au/programming/gsl-1.0/gsl-ref_16.html
Tyler,
I hope your problem is solved already. I am not a programming guru myself but I try to help. I think there are several points.
What you need is gsl_cdf_gaussian_P. The other thing (gsl_ran_lognormal) is inappropriate for two reasons.
1)It is a random number generator and not a cumulative distribution. That means it gives you numbers following a particular distribution, rather than a probability, as you need it.
2)It refers to the lognormal distribution, while you want the normal one.
Once you have a normal, cumulative distribution you can put the mean to 0 and the variance to unity to make it standard normal.
I hope this clarifies the situation. If not, I am here again in the morning.
Your function is for generating a random number with a lognormal distribution. If you are looking for the cumulative distribution you need to look in the "Special Functions" section of the GSL manual, section 7.15.