Fix and improve multiple places about random number generation and
shuffling