What you are asking for is a Logarithmic % Scale, everybody else has provided a Linear % Scale which is why we get values of 99.99% when providing 1/100,000.
It’s not really anymore complicated to calculate than the linear % scale, in fact, we are using the very same linear equation and just getting the base-10 log of the very same values, in the linear % we just wouldn’t use the base-10 log of those values.
The logarithmic scale is preferred here because now we are taking into context the magnitude of these values, thus allowing us to get percentages that are better representative of how close we are to these very small values.
Here is a quick script I typed out (with comments):
wait(3)
local MaxNum = 100 -- this is the max value generated by the randnum
local random = Random.new()
local RandNum = (random:NextNumber(0, 100)) -- this generates a random number (with too many decimal places) between 0 and 100
function roundNumber(num, numDecimalPlaces) -- this function will round the random number to the nearest decimal place specified
return tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num))
end
local Number = (roundNumber(RandNum, 4)) -- four is the amount of places the rounding function will round to (I know a millionth is 6 decimal places, but since we are looking for a percent, we would multiply it by 100, thus cancelling out 2 decimal places
if Number == MaxNum then -- If the number = 100 (VERY RARE)
print("One in a MILLION")
end
local ui = script.Parent -- I parented this local script to a frame
ui.Size = UDim2.new(Number/MaxNum, 0, 1, 0) -- I set the size of the frame (on the x axis) based off of how close the number is to 1 million
This script works just fine for me, lmk if this helps
P.S. I put this entire script in a while wait() do loop, and I calculated (without having to wait this long), that it would take over 4 HOURS using while wait() do, to generate the perfect number.
where the letter “n” is, which literally is the first letter of the word “num”, it is mentioned, another thing is you should be trying is to read the actual (rather simple) equation, I’ve made it as simple as possible for somebody to understand, where “n” is number and max/min are spelled out in words
Not only that but the number that YOU even said “1 / 100,000” is literally in the screenshot as well, you have to do better than this, just for the sake of the rest of us
You really need to formalize what you mean by “the distance from 0.000001”. Most people have assumed that you mean that the distance of some number x from 0.000001 is |x-0.000001|, or in code math.abs( x - 0.000001 ). You could normalize that to a percent just by dividing by the maximum distance, which is 100-0.000001, and then multiplying by 100 to make it a percent value. But this is not the only possible notion of distance.
If you work with logarithms, you’re basically considering distance to be how many orders of magnitude a number is from you goal. In this scenario, 0.00001 and 0.0000001 are both the same “distance” from 0.000001, they are both 1 order of magnitude different: one is 10 times too big, one is 10 times too small.
It’s not clear from your wording which distance metric you want: one of these, or something else entirely. If you want a log scale, you’re going to have a problem with making that into a percent, because while there are 8 orders of magnitude from 0.000001 to 100, there aren’t a finite number between 0 and 0.000001, because 0 is not a power of 10. You can’t do math.log10(0). Well, you can, you’ll just get inf, which you can’t do subtraction with or normalize to a percent. If you want to try to make this into a percent, you’d probably want to just clamp all values less than 10^-14 to be 10^-14, so that your “distance” is +/-8 powers of 10.
Of course, (1 / 100,000) would result in 0.999… and (1 / 10) would also result in a similar number. This is of course not what you were expecting and I knew myself that ideally this is not how it should be interpreted either, so I figured it was a scale issue.
Something I realized though is we were basically working with numbers that were to the power of 10. Your maximum 100 is 10^2, your minimum (1 / 1,000,000) is just 10^-6, both of your testing values were (1 / 100,000) or 10^-5 and (1 / 10) or 10^-1. So I knew the meaningful scale we should be operating in is the context of base-10.
And having already known that the logarithm is what we should use to get the power from a given value, it was natural to know log10 is what we were looking for.
Then it was just taking our simple equation from earlier, and wrapping each value with log10.
Glad you found your answer though and hope this helps explain the thought process a bit more!