Function with range equal to whole reals on every open set There is an example of a function that is unbounded on every open set. Just take $f(n/m) = m$ for coprime $n$ and $m$ and $f(irrational) = 0$.
I want to generalize this in a way to get a function which is not just unbounded on every open set, but whose range is equal to $\mathbb{R}$ on every open set. The latter construction clearly doesn't work.
I'm interested whether such function exists and if it exists is there any constructive way to define it?
 A: This is not really a new solution, but just a way to see one can come up with Jim's answer. The problem is equivalent to finding an equivalence relation on $\mathbb{R}$ such that each equivalence class is dense and there are $2^{\aleph_0}$ equivalence classes. To see this, suppose you have such an equivalence class $\equiv$. Consider the natural map $\pi: \mathbb{R} \to \mathbb{R}/\equiv$. Clearly the pre-image of every point is dense and now, you can post-compose $\pi$ with a bijection between $\mathbb{R}/\equiv $ and $\mathbb{R}$. The converse is similar.
Now, as $\mathbb{R}$ has the structure of an additive group, one can define equivalence relations by using subgroups: $x \equiv y$ iff $x-y$ is in a given subgroup $H$. Now if you use $H=\mathbb{Q}$, then you get the answer given by Jim Belk. You can use $H$ to be the subgroup of $2$-adic rationals. Then the $x$ is equivalent to $y$, if all but finitely many of their binary digits are equal, from which you can define 
A: As suggested by Gerry, Example 27 in Chapter 8 of Gelbaum and Olmstead's Counterexamples in Analysis is such a function.
A: A couple more ways of thinking about it. (There are connections with some of the answers above.)


*

*The graph of the function has to have the property that it intersects every horizontal line segment of positive length in the plane. Well-order these line segments such that each one has fewer predecessors than the cardinality of the reals, and then stick a point in them one at a time. At each stage, one has put fewer points into the graph than there are points in a line segment, so there will be points in the segment that are not vertically above or below points that are already chosen. When you've covered all the line segments, choose the remainder of the function arbitrarily.

*Enumerate all open intervals with rational end points. Now inductively create a graph as follows. Pick the first interval, and take a copy of the Cantor set inside it. Biject that copy of the Cantor set to the reals. Pick the second interval and find inside it an open interval disjoint from the Cantor set chosen earlier. Inside that, take a copy of the Cantor set and biject it to the reals. Keep going. The complement of the set where you've defined the function so far is always open and dense, so you can always continue. (It goes without saying that there's nothing special about copies of the Cantor set: any class of sets with cardinality that of the reals that is closed under countable intersections and does not include any open intervals will do the job.)
A: For a non-constructive solution, let $\pi : \mathbb{R} \to \mathbb{R}/\mathbb{Q}$ be the projection homomorphism, and let $g : \mathbb{R}/\mathbb{Q} \to \mathbb{R}$ be a bijection.  Then the composition $g\circ \pi$ has the desired property.
A: See Conway's base 13 function.
A: My instinct is to reach for Gelbaum and Olmstead, Counterexamples in Analysis, but it's in my office, and I'm at home. 
A: Once we have the idea of prodding the decimal expansion, there are any number of things we can do.  Here are a few.  We may assume we're starting with $0.a_1a_2a_3...$, e.g. by taking the fractional part of the input.  If any of the sums/limits mentioned below fails to exist, map the input to $0$.


*

*We construct the decimal digits of the image $b_0.b_1b_2b_3...$ one at a time.  Consider the alternating sum of the $a_i$ for $i$ odd.  If this converges (i.e. these $a_i$ are $0$ from some point on), then define $b_0$ to be this.  Next, look at the alternating sum of the $a_i$ with $i\equiv 2 \mod 4$.  If this converges to a number in $\{0,...,9\}$, then define $b_1$ to be this.  Then construct $b_2$ using the $a_i$ with $i\equiv 4 \mod 8$, and so on.

*Map the number to $\sum \frac{(-1)^{a_n}}{n}$.

*Map the number to $\lim_{n\to\infty} (a_1+...+a_n)/n$.  This will give us results only in $[0,9]$, but that's sufficient.
In each case, whatever tiny interval we're forced to start in, we can always get any output we like by using later digits.
A: You can even say something much stronger. As Gowers points out, you can reformulate the question as: Does there exist a function that intersect any vertical line, in any open interval? Here you can substitute "vertical line" with "continuous function" and "open interval" with "set with positive measure". The construction is exactly as Gowers point 1. You only have to use that the set of continuous function and the Borel $\sigma$-algebra each have the same cardinality as $\mathbb{R}$.
I wonder if you could substitute "continuous function" with "measureable function" in the above? How many measureable function are there?
A: Here is an explicit construction that uses only elementary functions and limits: define $f(x)=\lim_{n\rightarrow \infty}(n!\pi x)$ when that limit exists, and e.g. 0 otherwise. Notice that this $f$ is periodic with every rational number as its period. So it is enough to show that $f$ is surjective (and then you can take any preimge of any point and move it into any interval). To show that it's surjective, let $y\in \mathbb{R}$, and take $r\in \mathbb{R}$ to be such that $\tan(r\pi)=y$. Then one can check that the limit $\sum_{n=0}^\infty \frac{\lfloor rn\rfloor}{n!}$ is mapped by $f$ to $y$.
