then we want to generate from (a - alpha, a) U (b, b + alpha)
first, shift by the midpoint, aka, (a + b)/2. subtract all ranges by this midpoint value
(a - alpha - a/2 - b/2, a - a/2 - b/2) U (b - a/2 - b/2, b + alpha - a/2 - b/2)
(a/2 - b/2 - alpha, a/2-b/2) U (b/2 - a/2, b/2 - a/2 + alpha)
let y = b/2 - a/2. thus -y = a/2 - b/2. we can write
(-y - alpha, -y) U (y, y + alpha)
this is something we know how to solve. generate a random number R in (-alpha, alpha), then add y if R>0 or subtract y if R<0.
finally, to obtain the correct value, add y to our final result.
so the total code would be
float a =
float b =
float alpha =
float mid = (a + b) / 2;
float diff = (b - a) /2;
float r = Random.Range(-alpha, alpha);
if (r > 0) r += diff;
if (r < 0) r -= diff;
return r + mid;