Use random number of iteration for tests
Thanks @simonw for the review.
Related to #4361 and #4371.