How do I setup a formula to make ANY number into a number between X & Y ?

• Jun 22nd 2011, 01:54 PM
grasmanek94
How do I setup a formula to make ANY number into a number between X & Y ?
[Sloved]
Hello dear people,
I need some help out here because I'm a programmer and I want to make a script where the weather is predictable based on a given number (unix timestamp).

I am not bad at math but this goes a bit beyond my point of view,
what I want to do is to setup a formula to make any number into a number between chosen X & Y, for example:
Turn any number into a 'random but predictable' number between 20 an 30.

So for example if I want to turn number 1 using the 'formula' it would give the result 21
and if I do 2 it will go on till it reaches the max, then i I have the number 10 it begins again with 20 , or a random number between 20 and 30.
Can anyone help me out, please?
• Jun 22nd 2011, 03:02 PM
Soroban
Re: How do I setup a formula to make ANY number into a number between X & Y ?
Hello, grasmanek94!

Quote:

What I want to do is to setup a formula to make any number into a number
between chosen X & Y,

For example: turn any number into a 'random but predictable' number between 20 an 30.

So for example, if I want to turn number 1 using the 'formula' it would give 1.
And if I do 2, it will give 2, and so on till it reaches the max.
When I have the number 10, it begins again with 20, and so on.

Can anyone help me out, please?

I'll assume that we are dealing with integers (whole numbers).

Given a number $\displaystyle N$:

. . (1) Divide by 10 * and note the remainder.

. . (2) Add 20 to the remainder.

$\displaystyle \text{Example: }73$
. . $\displaystyle 73 \div 10 \:=\:7\text{, rem. }3$
. . $\displaystyle \text{Then: }3 + 20 \:=\:23$

$\displaystyle \text{Example: }198$
. . $\displaystyle 198 \div 10 \:=\:19\text{, rem. }8$
. . $\displaystyle \text{Then: }8 + 20 \:=\:28$

$\displaystyle \text{Example: }850$
. . $\displaystyle 850 \div 10 \:=\:85\text{, rem. }0$
. . $\displaystyle \text{Then: }0 + 20 \:=\:20$

* $\displaystyle \text{The "10" is the difference }30\!-\!20.$

• Jun 22nd 2011, 03:23 PM
Deveno
Re: How do I setup a formula to make ANY number into a number between X & Y ?
there are different ways to do this. it depends on the kind of behavior you want your "numbers" to have.

for example, you could take N, and turn it into X + (N mod (Y-X)) (assuming X and Y, but not necessarily N, are integers).

if X = 20, Y = 30, this would take 352.67 to 20 + 2.67 = 22.67.

or, you could take N, and turn it into X + (Y-X)/2 + ((Y-X)/2)erf(N) (where erf(N) is the error fnction: Error function - Wikipedia, the free encyclopedia)

(this will have the effect of making large -N close to X, and large N close to Y).

or, you could take N, and turn it into X + (Y-X)(e^(-N^2))

(this will make large and negative values for N close to X, and values close to 0 close to Y).

there are a LOT of ways to do what you describe, but they will perhaps not all produce the behavior you are looking for.
• Jun 23rd 2011, 02:30 AM
grasmanek94
Re: How do I setup a formula to make ANY number into a number between X & Y ?
Quote:

Originally Posted by Soroban
Hello, grasmanek94!

I'll assume that we are dealing with integers (whole numbers).

Given a number $\displaystyle N$:

. . (1) Divide by 10 * and note the remainder.

. . (2) Add 20 to the remainder.

$\displaystyle \text{Example: }73$
. . $\displaystyle 73 \div 10 \:=\:7\text{, rem. }3$
. . $\displaystyle \text{Then: }3 + 20 \:=\:23$

$\displaystyle \text{Example: }198$
. . $\displaystyle 198 \div 10 \:=\:19\text{, rem. }8$
. . $\displaystyle \text{Then: }8 + 20 \:=\:28$

$\displaystyle \text{Example: }850$
. . $\displaystyle 850 \div 10 \:=\:85\text{, rem. }0$
. . $\displaystyle \text{Then: }0 + 20 \:=\:20$

* $\displaystyle \text{The "10" is the difference }30\!-\!20.$

That sounds to me like a great way to deal with my problem, thank you :)

But yes this is a simple manner, now I have an additional optional question:
How can I make some 'ranges' for that? ex N >= 50 and N < 100 will make the result 1 and N >= 100 and N < 150 will make result 2 or something, Can this be done 'easily'?

it would be a bit silli in the game if the weather did change each second haha.