Suppose you have a number $N$ and an interval: $[\min, \max]$–where $\min$ and $\max$ are two known numbers creating a closed interval (including themselves).

I want to have a function that for a given $N$ will return the same $\text{result}$ number located in the specified interval ($\min \le \text{result} \le \max$), but preferably to cover the whole interval (at least, as much as possible).

Are there any known implementations/solutions/algorithms for this kind of problem?

I'm currently thinking if using modulo-n classes would be a solution:

$$f (N, \min, \max) = N \mathbin{\%} (\max + 1 - \min) + \min$$

($x \mathbin{\%} y$ being the rest of $x / y$)


\begin{align} f(42, 4, 6) & = 4 \\ f(43, 4, 6) & = 5 \\ f(44, 4, 6) & = 6 \\ f(45, 4, 6) & = 4 \\ & \ \ \vdots \end{align}

This is covering the entire interval which is great.

Can this be improved? What other solutions are there?

It's not a problem if there are multiple $N$ values for which the result is same as long the function is not a very general thing (e.g. constant) or the interval is not covered (possible return values are not part of the whole interval. e.g. last/first digit–that will be a value between $0$ and $9$, so obviously not something I'm looking for).

Currently I need only integers, but I'm open to see how this can be extended to real/complex numbers.

  • $\begingroup$ Could you give an example, as I feel your question is not very clear, for a given N in a range, gives you the same result, What do you mean by that? $\endgroup$ – Satish Ramanathan Dec 19 '15 at 20:13
  • $\begingroup$ @satishramanathan I updated the question. I'm a developer, and there we have a random function. I don't want this to be random or depend on anything that changes (e.g. time). I want for a given $N$ and range to get the same result now and after one day and after one year. $\endgroup$ – Ionică Bizău Dec 19 '15 at 20:37
  • $\begingroup$ @IonicăBizău if you want a random number generator to give you always the same sequence, use the same inital seed? Or what is your problem? $\endgroup$ – Adrian Dec 19 '15 at 22:38

A way to generalize this to the real numbers is to define a function


where the domain is $[0,b-a]$ and the range is $[a,b]$. If you want your program to output a value for any real number, simply have it subtract $b-a$ from your input until your input lies in the domain of $f(x)$.

Because the complex numbers contain two real components, any complex number $z$ in the interval $$\{z=x+iy\,|\, x\in[a,b],y\in[a,b]\}$$ can be mapped to $\mathbb{R}^2$ with the function $$g(x,y)=f(x)+if(y)$$ which has a domain $0\leq x \leq b-a,\ 0\leq y \leq b-a$.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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