The authors present a new molecular dynamics algorithm for sampling the isothermal-isobaric ensemble. In this approach the velocities of all particles and volume degrees of freedom are rescaled by a properly chosen random factor. The technical aspects concerning the derivation of the integration scheme and the conservation laws are discussed in detail. The efficiency of the barostat is examined in Lennard-Jones solid and liquid near the triple point and compared to the deterministic Nose-Hoover and the stochastic Langevin methods. In particular, the dependence of the sampling efficiency on the choice of the thermostat and barostat relaxation times is systematically analyzed.