# What function should I use to solve this? Simple question ;)

I need to write a simple function to be implemented in a computer program.

It should have as imput a number from 2 to 9 and gives as output a number from 1 to 5.

When the input is higher 6-7-8-9 the output should be lower 1-2. The opposite when the input is lower.

I can implement a random function inside it.

No need to satisfy any other particular requests. The important is that when I input 9 i got more low numbers as output then when I input 8 and so on.

Thanks! Sorry if this is a little bit vague!

• Are these numbers required to be integers? Or arbitrary floating point numbers? Feb 12 '12 at 12:53
• integers :) but I have also a function to round them ;) Feb 12 '12 at 12:54

Well, if the inputs are integers, then the solution is simple (provided I've understood you're question properly). Just pick an appropriate sequence $(z_{n})_{n = 2}^{9}$, where each $z_{n} \in \{1,2,3,4,5 \}$ and satisfying all your requirements (looks like it has to be decreasing, at least). Then define the function $f: \{ 2, 3, \ldots, 9 \} \to \{1,2,3,4,5\}$ by $f(n) = z_n$. For instance $(5,4,4,3,2,2,1,1)$ should satisfy your requirements, as far as I can tell. The function is then defined by $f(n) = z_{n}$, so $f(2) = 5, f(3) = 4, f(4) = 4, f(5) = 3, \ldots, f(9) = 1$. It is probably easiest to visualize as \begin{align*} & (2,3,4,5,6,7,8,9) \\ & (5,4,4,3,2,2,1,1) \end{align*} Each number in the top row is mapped to the one directly under it. This is also easily implemented in a computer program by using a simple, static array to look up function values. Or did you have something else in mind?
Edit: To add some randomness into the mix, we could, instead of assigning a single number to each input, assign a discrete probability distribution on $\{ 1,2,3,4,5\}$. The idea would then be that the input is paired with a random, uniformly distributed number $r$ on the unit interval $[0,1]$ (you can get these by calling an appropriate random number generator in your program). A discrete probability distribution on $\{1,2,\ldots, 5\}$ is nothing but a function $p: \{1,2,3,4,5\} \to [0,1]$ such that $$\sum_{n = 1}^{5} p(n) = 1.$$ So, for instance, if the input is $2$, and we want the function to produce the number $5$ half the time, the number $4$, say $40\%$ of the time and the number $3$ $10\%$ of the time, the function $p$ would be \begin{align*} & (1,2,\phantom{0.}3\phantom{0},\phantom{0.}4\phantom{0},\phantom{.}5\phantom{5})\\ & (0,0,0.10,0.40,0.5) \end{align*} Given an input of $2$, we use the assigned probability distribution and the randomly generated number $r$ to decide what the function value should be. In this case, we can use the following rules: $$f(2) = \begin{cases} 3 & \text{ if } 0 \leq r \leq 0.10, \\\\ 4 & \text{ if } 0.10 < r \leq 0.50, \\\\ 5 & \text{ if } 0.50 < r \leq 1. \end{cases}$$ Does this make sense to you? (Notice that the with of the intervals for $r$ is the same as the probability we assigned that particular number; this ensures that the events happen with the correct frequencies).
Edit: I'll add a quick note about implementation too, since the solution, as it stands, may be slightly awkward to deal with. I would suggest that instead of working with the probability distribution directly, use the cumulative probability distribution instead, i.e. $$P(x \leq n) = \sum_{i = 1}^{n} p(i).$$ For the distribution above, we would have the cumulative distribution function \begin{align*} &(1,2,\phantom{0.}3\phantom{0},\phantom{0.}4\phantom{0},\phantom{1.}5\phantom{0}) \\ &(0,0,0.10,0.50,1.00) \end{align*} So, given $r$, you determine $f(n)$ by simply walking this array of numbers in increasing order, and stop when you encounter a cumulative probability P such that $r \leq P$.