Disclaimer: I already asked this question once, however it was misunderstood and I was told by a moderator to repost it: Random number (between 0 & 1; > 5 decimal places) from binomial/beta-like distribution, with set mean (same as mode & median) and set variance
First of all this question is very similar to others linked just below. However, none of the answers I saw are correct/precise enough for a distribution with the same mean, mode and median.
Distribution that has a range from 0 to 1 and with peak between them? & Beta-distribution: how to generate a peak at certain mean value with a control on variance in extrems
What I need is a random number generator that draws from a distribution like the distributions below, for which I can set the mean and variance.
What I want the distributions to look like for a few example cases:
Notice that at 0.5 it should be like a truncated normal distribution and if the mean is smaller or bigger than 0.5 it should be a skewed distribution. And I want to be able to specify the variance and make it narrower or broader without it affecting the skewness/shape.
What I need it for: I am working on a stochastical model, which calculates the fraction of plant biomass eaten over whole plot which is made up of 100 cells. I then want to remove that fraction of plant biomass in each gridcell. However, I want to have some randomness in it so that it takes more from one gridcell and less from another etc. Thus I need to draw a random number based on the fraction and at the start of the model I want to specify the variance of the random fractions. I also want the fraction that I input to be the most likely number to be drawn (i.e. the mean is supposed to be the same as the mode).
Here are the rules the random number X needs to follow:
Conditions that X itself has to fulfill:
- X must be between 0 to 1 (including 0 and 1).
- X must have at least 5 decimal places (ideally unlimited, but 5 would be enough maybe even 4)
Conditions that the distribution from which X is drawn hast to fulfill
- The mean is also the mode (or at least better than with a beta distribution).
- The distribution should look like a binomial/beta distribution. So that it is skewed if the mean is above or below 0.5 (see first picture).
Input that I give:
- The mean can be any number between 0 and 1. At most I would want to round it to 4 digits but ideally I want it to be unchanged when I give it to the random number generator.
- I want to be able to set the variance freely, but relative to the mean (e.g. if mean is 0.6 variance should be 0.04 but if mean is 0.99 variance should be much smaller than 0.04). I want to set this variance once at the start.
Approaches I have tried and why they failed:
I have not managed to get either binomial distributions or beta distributions to work. I haven't found anything else. Here are the problems with either distributions. Maybe it will give you ideas. If you are interested in the code check my previous question: Random number (between 0 & 1; > 5 decimal places) from binomial/beta-like distribution, with set mean (same as mode & median) and set variance.
Problem with the binomial distribution:
It is not continuous i.e. I cannot set the variance independently from the number of decimal places the random number will have. E.g. if I want to have high variance I can do that by reducing the amount of events I simulate in the binomial distribution. For example only 6, however that means that I reduce my possible numbers to (0,0.2,0.4,0.6,0.8,1). If I increase the events then I get more decimal places; however the distrbution will be extremely narrow around the mean and look normally distributed.
Histogram with events = 5, n =100,000, mean 0.8. Has the variance I would want, but I cannot draw any numbers inbetween.
Problem with beta distribution:
The beta distribution allows me to keep variance the same (relative to the Mean) but change the mean freely. I have accomplished this by keeping beta always at a certain number and varying alpha, based on the mean. But the mean (red) does not coincide with the mode/maximum (blue) (nor median (purple)).
Do you have any ideas on how to draw a number from a distribution with a shape like the binomial one (see second picture) and with the same mean and mode, but with more decimal places. Is this even possible? I just need random numbers from that distribution, it doesnt need to be continous, I dont mind if it is just an approximation or if it is constructed from two distributions or if it's a workaround. I am writing a stochastical model so small differences from mean and mode would not be problematic as long there is no bias. I will be implementing this random number generator in C++ but answers in other languages are also fine like e.g. R.
Before you answer please double-check that your answer would consider my rules for the random number I specified above.