refactored random number generater code for groups