A new numerical procedure is presented to enable a hydrogen-fuelled two-stroke resonating free-piston generator target ideal Otto cycles to provide tracking paths for feedback control to maximise efficiency. Piston trajectories and design parameters are obtained for specified power, compression-ratio, and air-fuel-ratio. These parameters include scavenge port locations, heat release position, equivalent bottom-dead-centre position, and an electrical machine constant which is generally power-switched to take two values within a cycle. The main objectives of the paper are to develop the numerical procedure, and to demonstrate its use in finding a generator with maximum efficiency, and a generator with a single electrical machine constant, avoiding in-cycle power switching. To develop the procedure, a multi-physics generator model provides kinematics to an energy equation, culminating in a novel two-stage procedure. Stage-1 involves iteration and minimisation; Stage-2 just involves minimisation. Testing by simulation involves a 14-kW generator case study with compression ratio of 9, lean air-fuel equivalence ratio of 0.4, plus friction and electrical machine losses. An overall maximum generator efficiency of 54.9% is obtained using the procedure, compared to 51.3% without power switching. Computationally, the two-stage procedure proves highly efficient, typically taking around 11 min in total to complete on a desktop computer. The novelty of the procedure is its ability to optimally design and operate a hydrogen-fuelled resonating free-piston generator to meet a target specification, thereby offering a potentially inexpensive way to decarbonize transport, particularly in heavy duty applications.