In wall-bounded turbulent flow simulations, periodic boundary conditions combined with insufficiently long domains lead to persistent spanwise locking of large-scale turbulent structures. This leads to statistical inhomogeneities of 10% to 15% that persist in time averages of 60 eddy turnover times and more. We propose a shifted periodic boundary condition that eliminates this effect without the need for excessive streamwise domain lengths. The method is tested based on a set of direct numerical simulations of a turbulent channel flow, and large-eddy simulations of a high Reynolds number rough wall half-channel flow. The method is very useful for precursor simulations that generate inlet conditions for simulations that are spatially inhomogeneous, but require statistically homogeneous inlet boundary conditions in the spanwise direction. The method's advantages are illustrated for the simulation of a developing wind-farm boundary layer.