Quasi-exactly solvable periodic and random potentials