ID:1389433
 
rand(A, B) can only give you integers. If you want something more detailed than that, you'll need a way to get any number between any two numbers.

The thing
proc/randn(A, B) return lerp(A, B, rand())


(If you don't have a lerp() defined, you're doing something wrong, because it's freakin awesome, and this is only one reason why)
proc/lerp(A, B, C) return A + (B - A) * C

// or
proc/lerp(A, B, C) return A * (1 - C) + B * C


How it works
This works because of a form of rand() that isn't very commonly used.
If you don't provide any parameters, you get a random number between 0 and 1.
Keeping these facts in mind:
lerp(A, B, 0) == A
lerp(A, B, 1) == B

You should be able to figure out what a random number between 0 and 1 would get you.

If you can't, here's my explanation:
If C is a random percentage (from 0 to 100%),
then C% of the way from A to B means "a random percentage of the way from A to B",
or "a random number between A and B".

How I've used it
When I tinker with top-down shooters, I like to add a bit of randomness (inaccuracy) to where your bullet flies. Using rand(A, B) will only get you integers; a finite number of ways for the bullet to travel. This just isn't natural, and is really noticeable at long ranges (it's almost as bad as relying on rounded position/velocity).

Imagine standing a certain distance from a big wall, and aiming at the same point. If your gun has an inaccuracy of 2 degrees, your bullet will go one of five directions provided by rand(-2, 2):
angle + 0
angle +- 1
angle +- 2

You'll be hitting 5 points on the wall consistently. In other words, your bullets will have perfectly drilled 5 holes in the wall!

The solution to this is to use decimals to have extremely fine-detailed randomness. You can't possibly count how many angles are within 2 degrees:
angle - 2
angle - 1.999...9
...
angle + 1.999...9
angle + 2

So instead of 5 holes in your wall, you'll have an even spread.