Convert larger values into a smaller area

Unsure how to say it, but I want to convert a random value between 20 - 30 to 0 - 100. Like without changing its area in that range. How would I go about doing this?

Area? As in the area of a square?

An area like a range (1 - 100, 5 - 10)

ok if 21 is picked isn’t that between 0 and 100

what exactly are you trying to do???

2 Likes

are you talking about range() like in python???

1 Like

I may not be understanding this 100% but is it like the X value is 1-100 and the Y value is 5 - 10. So randomly it may be 25 (1-100) times 8 (5-10) to make an area of 200, and you want the same area with different random values?

Lets say a random value between 0 - 5 was made. (2) and now I want to convert that value to a range of 0 - 100 without changing the value. I would get 20

You can do this using linear interpolation (lerping).

local value = math.random(1,10)
local percent = value / 10 -- if it's 10, the percent would be 100%, if it's 5, 50% etc
-- we want to use 1-10 because it's easier to work with than 20-30, you can always just add 20 to have your random value between 20 and 30.

local lBound = 0
local uBound = 100
-- if value is 8, it would return 80 
local interpolatedValue = lBound + (uBound - lBound) * percent

This obviously works better with values that aren’t of 100, let’s say our new min is 27 and our new max is 59.

Old min would be 26, old max would be 48 and our target is 41. We would have to subtract 26 from each of our values to get a value between 26 and 41 to convert it to a percentile so our new bounds would be 0, 22 and our target would be 15

So, to get the percentage we do 15 (target - 26) / 22 (max - 26) which is 0.68181818181818 or about 68.2%

Now, to scale it up using linear interpolation,

local newLBound = 27
local newUBound = 59
local alpha = 0.682 -- remember this is our percentage from earlier which is about 68.2%

local interpolatedValue = newLBound + (newUBound - newLBound) * alpha
-- interpolatedValue is now 48.824
-- now if we do
local newAlpha = (interpolatedValue - newLBound) / (newUBound - newLBound) -- remember we have to subtract from the lower bound to get a percentage
print(newAlpha)
-- outputs 0.682 which is 68.2%

Divide the random number by 30, multiply that by 100.

local random = math.random(20,30) --say it gives us 25
local converted = (random/30)*100

How does this help?

function reMap(num, oldMin, oldMax, newMin, newMax)
    return (newMin + ((newMax - newMin) * ((num - oldMin) / (oldMax - oldMin))))
end

I’m on mobile so I might get something wrong.

1 Like

before I send you my answer are you trying to convert a fraction to using a different denominator

like 1/2 to 50/100? and the answer would be like 50

Yes

3303030303030303003 char limit

oh now I get it
thanks

this is super easy

let’s say we have the 2

2/5 convert to ?/100

what we do is take the second number’s denominator and divid it by the first number’s denominator which is 20

now what we do is take the first number (2/5) and multiply both the numerator and the denominator by 20

that would be 40/100
you would get 40

let me turn this into lua

function Range(Fraction, Denominator)
    local Divider = Denominator / Fraction[2]
    local Answer = Fraction[1] * Divider
    return Answer
end

Range({2, 5}, 100) -- the first value is a table(the numerator and the denominator), the second value is the denominator you want to convert it to. this would end up return with 40

this should convert a fraction with a different denominator

Basically, what I think OP is trying to do is he has a value from a range. Let’s say from 0 - 1 he has 0.5, which is 50%. He wants to take that 50% and place it in a new range(say 0-100), which would be 50.

I edited the code to have a comment explaining it