Fix math and randomization error

Bug 2751