Apex Random Function vs Real Random
Record Routing has become an art these days where companies want to route record in the most optimized ways. Some options are available when round robin routing.
1. Using SOQL offSet
2. Using Apex Random Function
3. Writing your own Round Robin Function
Using SOQL Offset
User randomUserSelected = Database.query('Select Id from User Limit 1 Offset' + Math.floor(Math.random() * getListActiveUser.size()).intValue());
Using Apex Random Function
User randomUserSelected = getListActiveUser[Math.floor(Math.random() * getListActiveUser.size()).intValue()];
Writing your Own Round Robin Function
I wrote my own Round Robin Function that uses DateTime of the last record assigned to a specific rep. I then order bu the Last_Record_Received__c field in ascending order and reps with no assignment (Last_Record_Received__c==null) will get assigned first.
This is how my query looks like
List returnActiveUserForRecord = Database.query(soqlUserQuery + ' where IsActive=true and ' + salesRepRoutingFieldName + '=true Order By Last_Record_Received__c ASC NULLS FIRST');
After each record assignment you must update the Last_Record_Received__c with the current DateTime. See code sample below:
user.Last_Record_Received__c= DateTime.Now(); update user;
With Option 3 you will get real round robin as each rep will get the same amount of records as I use the TimeDate as a flag of who last got a record. This helps with the more equal distribution of records that option 1 and 2 does not give you.