Need help on a statistical algorithm.

Bman28

New Member
I'm not great on statistics so I thought I would ask here.

For a project, I need to create a random number generator code. But I need the mean to be docile.

For instance. A RandomNumberGenerator that generates a float (Decimal) value between 0 and 1. But the results are skewed in favour of (For example) 0.1 when the mean is calculated. But if I changed a variable I can manipulate the mean to be a different number.

If this is something challenging then don't worry. I was just wondering if there's a known algorithm already for this as I can't find it anywhere. But if there is one, Could you please help explain all the variables as, Like I said at the start, I'm really not great with statistics maths, especially the terminology .

Thanks for taking the time to read, And I apologise if this was the wrong forum to post this.

AngleWyrm

Active Member
Java:
double shapedRNG(Random rng, double alpha, double beta){
double x = rng.nextDouble();
return x^(alpha-1) * (1-x)^(beta-1);
}

Last edited:

Dason

Java:
double shapedRNG(Random rng, double alpha, double beta){
double x = rng.nextDouble();
return x^(alpha-1) * (1-x)^(beta-1);
}
That's just returning the unnormalized pdf at a uniformly random point. That won't generate random draws from the distribution itself though.

Dason

What programming language are you using? Are you allowed to use libraries or does it *all* have to be from scratch?

Dason

Yeah AngleWyrm's code doesn't generate random samples from the beta distribution. It looks like Lua has some distributions programed in: https://scilua.org/sci_dist.html

Unfortunately the beta distribution isn't one that you can use the qrng on directly. If we could generate from the gamma distribution there is a fairly simple way to get draws from the beta as well... But it looks like the gamma distribution also won't allow qrng. If we had the inverse cdf (or even the cdf itself) it is pretty easy to do as well. But I don't see those available either.

Fortunately there are other methods. I would suggest implementing a rejection sampling scheme here since we have the pdf and can generate uniform random draws.

Dason

It looks like Lua has some distributions programed in: https://scilua.org/sci_dist.html

Unfortunately the beta distribution isn't one that you can use the qrng on directly.
Actually I might not have read closely enough. I think you can directly sample - you just need to provide a prng instead of a qrng. If that's the case it should be as easy as creating the distribution object and calling it's sample method with a prng of your choosing.

AngleWyrm

Active Member
To change location and scale of any value x
newX = (oldX - shiftLocationX) / scaleX

But the results are skewed in favour of (For example) 0.1 when the mean is calculated. But if I changed a variable I can manipulate the mean to be a different number.
skew = (mean - median) / standardDeviation
mean = median + skew * standardDeviation

To generate a normal distribution from a random variable, take a bell curve and flip it on it's side.

Last edited:

Dason

@AngleWyrm ....

Are you trying to help? There might be a crumb of usefulness in that post but almost every contribution to this thread that you've made is a net detriment in my mind. You post some things that are kind of related but aren't really useful to what they're trying to do. Maybe try adding some explanation to how it directly relates to the problem at hand here.

And seriously what is up with that normal distribution flipped on it's side? What's the point of that?

Dason

@Bman28 let me know if my suggestions on how to do it in Lua help or not. I'm not sure if any of that requires any external packages or whatever because I don't use Lua but wanted to help.

katxt

Well-Known Member
The triangular is another simple function. Put b = 0 (bottom), t = 1 (top) and set M = mode
Function tri(b, M, t)
If (t - b) * Rnd < M - b Then tri = b + (M - b) * Rnd ^ 0.5
Else: tri = t - (t - M) * Rnd ^ 0.5
End If
End Function

Dason

Honestly that's probably a better suggestion for this. Going the full Beta route might be nicer if it's possible to code easily but the triangle distribution would probably work quite well here. I guess it all comes down to the specifics of what attributes are necessary from the distribution.

katxt

Well-Known Member
If you can mathematically define a shape you like for your distribution, it's easy to generate random numbers from it. It doesn't even have to have an area of 1. If the high point is M, then generate a pair of uniform numbers, x from 0 to 1, and y from 0 to M. If y is under your graph keep x. If not, try again with a new pair.

spunky

Can't make spagetti
@AngleWyrm ....

Are you trying to help? There might be a crumb of usefulness in that post but almost every contribution to this thread that you've made is a net detriment in my mind. You post some things that are kind of related but aren't really useful to what they're trying to do. Maybe try adding some explanation to how it directly relates to the problem at hand here.

And seriously what is up with that normal distribution flipped on it's side? What's the point of that?
^^ OMG THIS^^ SO MUCH OF THIS.

spunky

Can't make spagetti
The triangular is another simple function. Put b = 0 (bottom), t = 1 (top) and set M = mode
Function tri(b, M, t)
If (t - b) * Rnd < M - b Then tri = b + (M - b) * Rnd ^ 0.5
Else: tri = t - (t - M) * Rnd ^ 0.5
End If
End Function
Honestly that's probably a better suggestion for this. Going the full Beta route might be nicer if it's possible to code easily but the triangle distribution would probably work quite well here. I guess it all comes down to the specifics of what attributes are necessary from the distribution.
Isn't the convolution of of 2 iid uniforms the triangular (or tent) distribution? Even just having a simple uniform generator over [0,1] would get you the triangular distr through convolutions.