A randomness-efficient sampler for matrix-valued functions and applications