# How to convert a number from one min\max set to another min\max set? [closed]

I'm doing terrain generation and I have a perlin library that is giving me random numbers between -1 and +1. I want to convert this to the scale of 0-255. What is the proper way to do this?

-

## closed as off-topic by Seth Battin, Anko, Josh Petrie♦May 11 '15 at 15:54

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – Seth Battin, Anko, Josh Petrie
If this question can be reworded to fit the rules in the help center, please edit the question.

Base formula is:

``````Result := ((Input - InputLow) / (InputHigh - InputLow)) * (OutputHigh - OutputLow) + OutputLow;
``````

Result := ((Input - -1) / (1 - -1) * (255 - 0) + 0;

From here you can optimize the conversion if your coefficients are static, but compiler will probably do it by itself as well.

Result := ((Input - -1) / 2) * 255 + 0;

Result := Input * 127.5 + 127.5;

-
This answer made the most sense to my programmer brain. :) A "thank you" to everyone else for their great answers. – Thraka Aug 2 '12 at 15:44

The range `-1` to `1` is a total range of `2`. So you need to scale it appropriately to get to `255`. Something like this:

``````(randomPerlin + 1.0) * 127.5
``````

So you first add `1`, to get to `0..2` then multiply with `127.5` to get a range of `0..255`

-

Through the power of scaling and bias.

From your value, subtract the minimum value of your range. That will give you a value in the range [0..2].

Divide that by the width of the source range, giving you a value in the range [0..1].

Multiply that by the width of the target range, giving you a value in the range [0..255].

Add the base of the target range to get a value in the target range, which for this case is the same as the previous step.

In summary:

``````            (v - (-1.0))
v' = 0 + ---------------- * (255 - 0)
(1.0 - (-1.0))
``````
-

Translate the input range so we get the min to zero by adding 1 (the negative value of the min input) -1 .. 1 -> 0 .. 2

As the output range starts with zero, do nothing for that.

Scale the new input range so it fits the output range, this is easy as they now both starts at zero: multiply the value by 255/2 0..2 * 2/255 -> 0..255

Done!

Example:

0.5 will go: (0.5 + 1) * 127.5 = 191.25 -0.5 will go: (-0.5 + 1) * 127.5 = 63.75

-

Lets say `x` is original input and `y` is expected output. We have at least two points (x,y) (-1, 0) (1, 255)

Since we want to do a linear interpolation we can look into the equation of form `y = m*x + c`

With above two points, we can solve for `m` and `c` Once that is done, you get the transform equation.

Note this method works for other types of equations too. e.g If you want a exponential or quadratic interpolation.

-
In our field, the common meaning of linear interpolation tends to be that you mix linearly between two quantities with a blending factor like this: `v = (1-a)*x + (a)*y`. I don't see how this answer has much relevance to the question asked as all he wants is a range mapping. – Lars Viklund Aug 4 '12 at 11:08
the thing is two points in a plane uniquely define a line. we can treat range mapping as straight line. and we know two points on that line. solving it will give line equation. may be linear interpolation is not the right word here, but I wanted to mean set of inner points between those two points on this line. – Ankush Aug 4 '12 at 13:23
``````oldRange = oldMax - oldMin
newRange = newMax - newMin

newValue = ((oldValue - oldMin) * newRange / oldRange) + newMin
``````

Hope this helps.

-