How do you generate random number intervals?

I’m trying to generate some random number intervals and I tried searching the forums but couldn’t find anything.
Example: generating random numbers between 0.5 to 2.5 (inclusively) would generate 0.5, 1.0, 1.5, 2.0, and 2.5.

Can anyone help?

Random.Range (1, 6) / 2.0;

–Eric

Don’t you need to typecast that, Eric?

float result = (float)Random.Range(1,6) / 2f;

Otherwise I think your formula would give you just 0, 1, or 2.

(Also in case you’re doing this hundreds of times a frame multiplying by 0.5f would be better than dividing by 2, performance-wise. Albeit by a tiny amount.)

Nope. “Random.Range (1, 6) / 2f” already returns a float.

Not necessarily…on my CPU, division and multiplication in Unity (and addition and subtraction) are the exact same speed. The whole “division is slower” thing stopped being true years ago. Also, even square roots are only a few times slower than standard math operations now so typically there’s barely a reason to avoid them.

–Eric