We study the mean square of sums of the kth divisor function dk(n) over short intervals and arithmetic progressions for the rational function field over a finite field of q elements. In the limit as q→∞q→∞ we establish a relationship with a matrix integral over the unitary group. Evaluating this integral enables us to compute the mean square of the sums of dk(n) in terms of a lattice point count. This lattice point count can in turn be calculated in terms of a certain piecewise polynomial function, which we analyse. Our results suggest general conjectures for the corresponding classical problems over the integers, which agree with the few cases where the answer is known.