use local rand instead of global rand (thanks to tip from Sebastien Binet)