# Generate number from given range

Your function must accept three numbers in any order:

• A signed real number (or ±Infinity) - The number (N) for which you have to generate a number from the range
• A signed integer - Minimum of the range
• A signed integer - Maximum of the range

Input:

• The minimum of the range will always be less than the maximum of the range, so don't worry about it
• N can be any real (if it is possible Infinity and -Infinity also can be passed)

Requirements:

• Your function must output same result each time for same inputs
• Your function must output different result each time for different N (except when it's due to a floating point error)
• The output must be in the given range
• The outputs of your function for all numbers N must cover the entire range (except when missing outputs are due to floating point error)

• The order of the input
• Is it possible to pass ±Infinity as a N?
• Are the minimum and maximum of the range inclusive or exclusive?

Unable to provide test cases because there are many ways to solve this problem, so please provide your test cases.

The shortest code in each programming language wins!

• This question just seems to be "find a mapping between a value from (-inf, inf) to (a,b)? Mar 6 at 15:59
• You also said distribution was linear even though you can't linearly map to infinity Mar 6 at 16:31
• Do -0 and +0 have to return different results? Mar 7 at 6:58
• Do answers get to decide if ±Infinity are valid inputs, or is it dependent on whether the language allows them? Mar 7 at 7:08
• You could simply add: “The outputs of your function for all numbers N must cover the entire range (except when missing outputs are due to floating point error).” Mar 9 at 20:17

# Python, 29 bytes

lambda a,b,c:a+(b-a)/(1+2**c)


Attempt This Online!

Does not include the upper bound.

Uses the mapping $$f(c) = \frac{a-b}{1+2^c}+a$$

An interactive graph of what this looks like: # Wolfram Language (Mathematica), 33 bytes

N@Rescale[#1,{-∞,∞},{#2,#3}]&


Try it online!

Mathematica easily operates with infinities both signs, so here it is. For input infinite values you can use \[Infinity] or ∞ with sign.

# R, 31 bytes

\(w,a,b)(.5+atan(w)/pi)*(b-a)+a


# Thunno, $$\ 10 \log_{256}(96) \approx \$$ 8.21 bytes

-s2@1+/A$+  Attempt This Online! -s2@1+/A$+ # stack is min, max, wind
-          # subtract
s         # swap, wind is on top
2@       # 2 ^ wind
/    # divide (max-min) by (1 + 2^wind)
A$+ # add the first input  ## Thunno, $$\ 6 \log_{256}(96) \approx \$$ 4.93 bytes -DZs*$


Attempt This Online!

This uses the formula ((b - a) * c) % (b - a), where a is the minimum of the range, b is the maximum of the range, and c is the wind. I noticed after posting this that the formula doesn't really work though.

-DZs*$# stack is min, max, wind - # subtract D # duplicate Zs # swap bottom two * # multiply$ # swapped modulus

• It's not going to be different for any values if a = 0, b = 1, e.g. Mar 6 at 15:55
• @97.100.97.109 Thanks, not sure what I can change this to Mar 6 at 15:58
• You could, e.g. use a logistic function which is scaled to range between the given values? Mar 6 at 15:58
• Value -100 produce output out of range. This general problem, it's not possible to do rescale without information about min and max of input Mar 6 at 16:07
• @lesobrod I'm working on a new way that will work better Mar 6 at 16:10

# Pyt, 14 bytes

Ť←←Đ3Ș⇹-⇹↔⁺₂*+


Try it online!

Takes inputs as c b a (each on their own line)

Uses tanh as the mapping function

Ť                       implicit input (c); Ťanh(c)
←←                     get input twice (b, then a)
Đ                    Đuplicate a
3Ș                  Șwap top three items of stack
⇹                 swap top two on stack
-                subtract (b-a)
⇹               swap top two on stack
↔              flip the stack
⁺             increment [tanh(c)+1]
₂            halve [(tanh(c)+1)/2]
*           multiply [(b-a)*(tanh(c)+1)/2]


# Charcoal, 13 bytes

ＮθＩ⁺θ∕⁻Ｎθ⊕Ｘ²Ｎ


Try it online! Link is to verbose version of code. Explanation: Another port of @adam's Python answer.

Ｎθ              First input as a number
Ｎ        Second input as a number
⁻         Minus
θ       First input
∕          Divided by
²    Literal integer 2
Ｘ     Raised to power
Ｎ   Third input as a number
⊕      Incremented
⁺            Plus
θ           First input
Ｉ             Cast to string
Implicitly print


Originally this was meant to be based on @KiptheMalamute's answer, so I started with:

$$f(c) = a + \frac {2(b - a)} {1 + \tanh c}$$

Charcoal doesn't have hyperbolic (or indeed trigonometric) functions, so I rewrote this in terms of the exponential function:

$$f(c) = a + \frac {b - a} {1 + e ^ {-2 c}}$$

But there's no particular reason to use $$\ e^{-2} \$$; any base will do, such as $$\ 2 \$$, which is much golfier:

$$f(c) = a + \frac {b - a} {1 + 2 ^ c}$$