Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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!

share|improve this question
    
Are these numbers required to be integers? Or arbitrary floating point numbers? –  Martin Wanvik Feb 12 '12 at 12:53
    
integers :) but I have also a function to round them ;) –  Tom Dwan Feb 12 '12 at 12:54

1 Answer 1

up vote 1 down vote accepted

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$.

share|improve this answer
    
I wanted the function to have some grades of randomness, if I put as input the value 2, the most of times it has to return 5, a little less frenquenlty it should return 4, and rarely 3... –  Tom Dwan Feb 12 '12 at 13:18
    
Ok; I was wondering about that sentence "I can implement a random function inside it". That makes things slightly more complicated (and the result isn't really a proper mathematical function anymore, but that is of no consequence). Let me edit the post a bit. –  Martin Wanvik Feb 12 '12 at 13:24
    
@Giorgio: Better? –  Martin Wanvik Feb 12 '12 at 13:54
    
Much better :D Thank you!!! –  Tom Dwan Feb 12 '12 at 16:23
    
@Giorgio: You're welcome. I've added a small note about implementation too, which you may (or may not) find helpful. –  Martin Wanvik Feb 12 '12 at 18:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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