Based on the comment that you want both lower and higher numbers to have a higher likelihood:
// let u,v be random real numbers from [1, 10]
x = log(u) // x is from 0.0 to 1.0, with higher probability getting higher values.
y = 1 - log(v) // y is from 0.0 to 1.0, with higher probability of getting lower values.
if abs(x - 0.5) > abs(y - 0.5):
return x
else:
return y
This is a bit hacky, but it makes it extremely unlikely to get a value exactly in the middle 0.5
and likely to get the edge values. It seems to fit your requirements though.
EDIT: It could use some fine tuning when x == y
. You could use another random choice to determine which to select in that case:
if x == y:
// let w be a random number either 1 or 2.
if w == 1:
return x
else:
return y
Additionally, if the application calls for this function to be a bit more or less polar, it can be adapted to do so:
// let u,v be random real numbers from [1, K] where K > 1
// and let j be a real number given by log(K). j is selected by the function's caller.
x = log(u) / j
y = (1 - log(v)) / j
// The remainder of the formula is identical.
By using a value like 2 for j, K = 100 and thus is more likely to be a polar value. If a value less than 10 is used for j, it will be less likely to be a polar value. In this way you can control the "slope" of the function.