Smart radio environments (SREs) are seen as a key rising concept of next generation wireless networks, where propagation channels between transmitters and receivers are purposely controlled. One promising approach to achieve such channel flexibility relies on semipassive reflective Reconfigurable intelligent surfaces (RISs), which can shape the bouncing multipath signals for enhancing communication quality of service, making localization feasible in adverse operating conditions, or reducing unwanted electromagnetic emissions. This paper introduces a generic framework that aims at optimizing the end-to-end precoder controlled by RISs, so that arbitrary beam patterns can be generated, given a predefined lookup table of RIS element-wise complex reflection coefficients. This method is validated and illustrated for different targeted beam patterns in both the far-field and the near-field regimes, while considering the prior characterization of real-life RIS hardware prototypes. These results show how, and to which extent, RIS configuration optimization can approximate the desired beams under realistic hardware limitations and low-complexity implementation practicability, or conversely, which RIS elements' lookup tables would be more suitable. The latter can provide useful guidelines for future RIS hardware designs.