Markov chain Monte Carlo (MCMC) relies on efficient proposals to sample from a target distribution of interest. Recent optimization-based MCMC algorithms for Bayesian inference, e.g., randomize-then-optimize (RTO), repeatedly solve optimization problems to obtain proposal samples. We interpret RTO as an invertible map between two random functions and find that this mapping preserves the random functions along many directions. This leads to a dimension independent formulation of the RTO algorithm for sampling the posterior of large-scale Bayesian inverse problems. We applied our new methods on Hierarchical Bayesian inverse problems.