Deterministic and stochastic algorithms for mechanical systems under constraints