Towards math freaks.

(music was my first love, math will be the last)

Lets build the formula for random step by step.

Random(c)

c? Yes i dont use 'x' for now. To make you understand that the number we feed to the random function is not coordinate, or height, or any absolute number.

But a count of numbers.

If we need a number between 5 and 7, the count of numbers between them is 3, namely 5, 6 and 7.

I am aware that i leave out the no "whole numbers" to simplify.

So 'c' stands for a count of numbers.

The count of numbers between 3 and 10 = 8. (3 4 5 6 7 8 9 10)

Or to stay in math. c = The end number - starting number + 1

e = end number

s = starting number

c= e - s + 1

so now we are at

Random(e-s+1)

Need a number between 0 and 1 ?

Random(1 - 0 + 1)

or

random(2)

Need a number between 0 and 7 ?

random(7 - 0 + 1)

or

random(8)

But that dont give us the answer for when we need a number between 5 and 7.

To solve this we random the "count of numbers" and add this to the start number.

or

Random(e-s+1) + s

so again, need a number between 0 and 7 ?

random(7 - 0 + 1) + 0

or

random(8)

but if we need a number between 1 and 7 ? !!

random(7 - 1 + 1) + 1

or

random(7) + 1

need a number between 50 and 60 ?

random(60 - 50 + 1) + 50

or

random (11) + 50

there are 11 numbers between 50 and 60 + the 50 where we want to start of with.

But now there is a last thing to bring into the formula. The steps.

Suppose we need a number between 0 and 10, but in steps of 2.

That are the numbers 0, 2, 4, 6, 8 and 10.

The count of numbers is here 6. (You can point with you mouse to the numbers and count them.)

Allow me to use 'd' to represent the steps (the greec delta), i have "s" in use already.

so again.

c = ((End number - Start number) / the steps) + 1

or

random(c) will be

random( ( (e-s) / d ) + 1)

And just like when we want the result to start counting at a certain number.

We need to translate the count of numbers back to absolute numbers

or

random( ( ( ( (e-s) / d ) + 1 ) ) * d ) + s

need a number between 0 and 10 in steps of 2 ?

e = 10

s = 0

d = 2

random ( ( ( ( (e-s) / d ) + 1 ) ) * d ) + s

random ( ( ( ( (10-0) / 2 ) + 1 ) ) * 2 ) + 0

random ( ( ( ( ( 10 ) / 2 ) + 1 ) ) * 2 ) + 0

random ( ( ( ( 5 + 1 ) ) * 2 )

random ( 6 ) * 2

random (6) * 2

need a random number between 10 and 630 in steps of 10 ?

640 could be width of the screen.

We stay 10 pixels from the left edge. And 10 pixels from the right edge.

So we need a number between 10 and 630 in steps of 10

random ( ( ( ( (e-s) / d ) + 1 ) ) * d ) + s

random ( ( ( ( (630-10) / 10 ) + 1 ) ) * 10 ) + 10

or

random((63) * 10) + 10

Hope this helped some out.