I have generated a number z from a range (x,y) and want to map z to another range (p,q)

This can be achieved using linear interpolation as seen here: https://www.mathworks.com/matlabcentral/answers/379380-mapping-of-a-random-number-in-one-range-to-another-range

and here:


The relationship I am trying to achieve is similar to this:

01, 02, 03, 04, 05, 06, 07, 08, 09, 10
25, 25, 25, 26, 26, 26, 27, 27, 27, XX

Where the x,y is the range 1-10, p,q is 25-27

If z=7, than the 'equivalent' in the second range would be roughly 27, however this would require one of the intervals to be rounded up or down so it's not totally accurate.

My question is, will linear interpolation achieve the above relationship, and if so, does the relationship 'preserve' the randomness?

For example, if I were to have rolled a die, does the second, interpolated value represent the same 'roll' so to speak, but in the context of a die with more sides?

  • $\begingroup$ Are the ranges discrete or continuous? If both discrete, are the numbers of entries the same? $\endgroup$ Commented Nov 8, 2019 at 22:26
  • $\begingroup$ @herbsteinberg the ranges are discrete, the number of entries in each range are different. $\endgroup$ Commented Nov 8, 2019 at 22:30

2 Answers 2


If you need a deterministic function to map between the two intervals, then in general, as Ethan Bolker points out in his answer and attached comments, one cannot create a perfectly satisfactory map. It can only be done when the number of input values is an integer multiple of the number of output values.

If, however, you can use a probabilistic function, then it can be done, by mapping some input values to two or more output values, in a way that preserves the appropriate probabilities.

For example, suppose you want to map $\{1, 2, 3, 4, 5, 6, 7, 8, 9, 10\}$ to $\{25, 26, 27\}$ in a way that preserves the uniformity and also the ordering of values, as much as possible. That is, we want to define our mapping $f$ so that

  • If $x < y$, then $f(x) \leq f(y)$
  • If $x > y$, then $f(x) \geq f(y)$

Then, we would say $f(1) = f(2) = f(3) = 25$, $f(5) = f(6) = 26$, and $f(8) = f(9) = f(10) = 27$. In addition, we have the following random variables:

$$ f(4) = \begin{cases} 25 & \text{with probability $1/3$} \\ 26 & \text{with probability $2/3$} \end{cases} $$


$$ f(7) = \begin{cases} 26 & \text{with probability $2/3$} \\ 27 & \text{with probability $1/3$} \end{cases} $$

This should be straightforward to do in code.


The answer you have accepted to the linked stackoverflow question calls on the simple algebra for linear interpolation.

Linear interpolation from one range of integer values to another will almost always require rounding. Asking whether the rounded value represents "the same roll" is not a mathematical question. The answer will depend on context.

Rounding can't preserve "randomness". The chances of $1$, $2$ and $3$ on a three sided die are $1/3$ each. There is no way to map those to the range for a $5$ sided die that produces each answer with probability $1/5$.

You won't need rounding just when the number of integers in the domain is a divisor of the number of integers in the codomain - for example, converting a roll of a $6$ sided die to one of $12$ sides. Then you map $r$ to $2r$. You get all the even rolls with equal probability, but never the odd ones.

  • $\begingroup$ You may be right that it is not a math question, and 'preserve randomness' may be a poor choice of words. What I'm trying to do is compare the effect of two different random ranges (or a change on one) by using the same random value as a control. If that makes sense. $\endgroup$ Commented Nov 8, 2019 at 22:42
  • $\begingroup$ I'm just not certain whether linear interpolation of that value will achieve that aim $\endgroup$ Commented Nov 8, 2019 at 22:42
  • $\begingroup$ There is no reasonable way to compare a random roll of a three sided die to a random roll of a five sided die. Linear interpolation with rounding won't work - but nothing else could work either. $\endgroup$ Commented Nov 8, 2019 at 22:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .