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


  • 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)


  • 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)

Please specify:

  • 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!

  • 4
    \$\begingroup\$ This question just seems to be "find a mapping between a value from (-inf, inf) to (a,b)? \$\endgroup\$ Mar 6, 2023 at 15:59
  • 3
    \$\begingroup\$ You also said distribution was linear even though you can't linearly map to infinity \$\endgroup\$
    – mousetail
    Mar 6, 2023 at 16:31
  • 2
    \$\begingroup\$ Do -0 and +0 have to return different results? \$\endgroup\$ Mar 7, 2023 at 6:58
  • 2
    \$\begingroup\$ Do answers get to decide if ±Infinity are valid inputs, or is it dependent on whether the language allows them? \$\endgroup\$ Mar 7, 2023 at 7:08
  • 2
    \$\begingroup\$ 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).” \$\endgroup\$ Mar 9, 2023 at 20:17

6 Answers 6


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:

graph of the above function


Wolfram Language (Mathematica), 33 bytes


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


Attempt This Online! or Try it at rdrr.io with graphical output


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


Port of's Python answer

Attempt This Online!

-s2@1+/A$+ # stack is min, max, wind
-          # subtract
 s         # swap, wind is on top
  2@       # 2 ^ wind
    1+     # add one
      /    # divide (max-min) by (1 + 2^wind)
       A$+ # add the first input

Thunno, \$ 6 \log_{256}(96) \approx \$ 4.93 bytes


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
  • \$\begingroup\$ It's not going to be different for any values if a = 0, b = 1, e.g. \$\endgroup\$ Mar 6, 2023 at 15:55
  • \$\begingroup\$ @ Thanks, not sure what I can change this to \$\endgroup\$
    – noodle man
    Mar 6, 2023 at 15:58
  • \$\begingroup\$ You could, e.g. use a logistic function which is scaled to range between the given values? \$\endgroup\$ Mar 6, 2023 at 15:58
  • \$\begingroup\$ 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 \$\endgroup\$
    – lesobrod
    Mar 6, 2023 at 16:07
  • \$\begingroup\$ @lesobrod I'm working on a new way that will work better \$\endgroup\$
    – noodle man
    Mar 6, 2023 at 16:10

Pyt, 14 bytes


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]
             +          add [a+(b-a)*(tanh(c)+1)/2]; implicit print

Charcoal, 13 bytes


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

Nθ              First input as a number
       N        Second input as a number
      ⁻         Minus
        θ       First input
     ∕          Divided by
           ²    Literal integer `2`
          X     Raised to power
            N   Third input as a number
         ⊕      Incremented
   ⁺            Plus
    θ           First input
  I             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} $$


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.