How to find a function from reals to reals such that the image of every open interval is the whole of R?
Is there one which maps rationals to rationals?
How to find a function from reals to reals such that the image of every open interval is the whole of R?
Is there one which maps rationals to rationals?
Though this can be done explicitly with enough cleverness (for example with the Conway base 13 function), I rather like the following choice-based argument, which requires almost no thought once you're familiar with transfinite recursion.
Consider the set consisting of all ordered triples of reals $(a,b,c)$ with $a<b$. This has the cardinality of the continuum. By the axiom of choice, it is well-orderable; pick a well-ordering of smallest possible order type, so that every triple has less than continuum-many predecessors.
Now we build our function $f$ recursively along this well-order, fixing the value of $f$ at one new point at each step. At the step corresponding to $(a,b,c)$, we want to ensure that there exists a point $x$ in the open interval $(a,b)$ such that $f(x)=c$. Since we've only fixed $f$ at less-than-continuum-many points, and $(a,b)$ has cardinality continuum, we can choose an $x$ in $(a,b)$ such that $f(x)$ is not yet fixed, and fix $f(x)$ to be $c$.
This recursion gives us a partial function from $\mathbb{R}$ to $\mathbb{R}$ that already satisfies our requirements. We can make it total by just setting $f(x)$ to be $0$ (say) wherever $f(x)$ is not yet defined.
If we additionally want $f$ to map rationals to rationals, we can simply set $f(x)=0$ for every rational $x$ before commencing the recursion.
With a minor modification, Qiaochu’s deleted answer works fine. Let $\mathfrak{C}=\left(\Bbb R/\Bbb Q\right)\setminus\{\Bbb Q\}$, and let $h:\mathfrak{C}\to\Bbb R$ be a bijection. Each coset $C\in\mathfrak{C}$ is dense in $\Bbb R$, so $C\cap(a,b)\ne\varnothing$ for each open interval $(a,b)$ in $\Bbb R$ and $C\in\mathfrak{C}$. Now define
$$f:\Bbb R\to\Bbb R:x\mapsto\begin{cases} h(x+\Bbb Q),&\text{if }x\in\Bbb R\setminus\Bbb Q\\ x,&\text{if }x\in\Bbb Q\;; \end{cases}$$
then $f[I]=\Bbb R$ for every non-empty open interval $I$ in $\Bbb R$, and $f[\Bbb Q]=\Bbb Q$.
A slightly different approach is to think of $\Bbb R$ as a vector space over $\Bbb Q$ and let $B$ be a basis for $\Bbb Q$. Without loss of generality assume that $1\in B$. $|B|=|\Bbb R|$, so there is a bijection $h:B\setminus\{1\}\to\Bbb R$. Define
$$f:\Bbb R\to\Bbb R:x\mapsto\begin{cases} h(b),&\text{if }x\in b\Bbb Q\setminus\{0\}\text{ for some }b\in B\setminus\{1\}\\ 0,&\text{otherwise}\;; \end{cases}$$
$b\Bbb Q\cap c\Bbb Q=\{0\}$ whenever $b,c\in B$ with $b\ne c$, so $f$ is well-defined. For each $b\in B$ the set $b\Bbb Q$ is dense in $\Bbb R$, so $f[I]=\Bbb R$ for every non-empty open interval $I$ in $\Bbb R$. Finally, $f[\Bbb Q]=\{0\}\subseteq\Bbb Q$. If it’s desired to make $f[\Bbb Q]=\Bbb Q$, just modify the definition of $f$ slightly:
$$f:\Bbb R\to\Bbb R:x\mapsto\begin{cases} h(b),&\text{if }x\in b\Bbb Q\setminus\{0\}\text{ for some }b\in B\setminus\{1\}\\ x,&\text{if }x\in\Bbb Q\\ 0,&\text{otherwise}\;. \end{cases}$$
Of course the same basic idea works with any family of $2^\omega$ pairwise disjoint dense subsets of $\Bbb R$.
(But I find transfinite recursion the most natural approach.)
First construct a nowhere-dense perfect set (i.e. a compact set with no isolated points) $E \subset (0,1)$ that contains no rationals. This is not hard to do by a modification of the construction of the Cantor set. Note that $E$ has the cardinality of the continuum, and in fact it's not hard to construct a function $f_E$ from $E$ onto $\mathbb R$.
Now enumerate the intervals with rational endpoints as $(a_i, b_i)$. We can take sets $E_i \subset (a_i, b_i)$ that are pairwise disjoint, of the form $E_i = s_i + t_i E = \{s_i + t_i e: e \in E\}$ where $s_i$ and $t_i$ are rational and $t_i > 0$: in fact to choose $s_i$ and $t_i$ it suffices to have $[s_i, s_1 + t_i] \subset (a_i, b_i)$ disjoint from $\bigcup_{j=1}^{i-1} E_i$, which can be done since the latter is nowhere dense. Now for $x \in E_i$ take $f(x) = f_E((x - s_i)/t_i)$. Thus $f$ maps $E_i$ onto $\mathbb R$, and since every open interval contains some $E_i$ the image of such an interval is all of $\mathbb R$. Now define $f$ however you want on the complement of $\bigcup_i E_i$ (in particular you could take $f$ to be rational on the rationals).