You need to sign in to do that
Don't have an account?

How to select random element from Set<String>
Set<String> setUserID = new Set<String>{'1','55','80','12', '6','22','87'};
Now I want to select random element from it. Below random method is not yet exist. So how can I select random element from this set. Any other idea?
System.debug('Random Set Element = ' +setUserID.random());
You really have two options.
The first option is to generate a random index, convert the set to a list, and fetch the list element using the bracket notation.
The second option is to generate the random index, then iterate through the Set until you get to the appropriate element.
All Answers
Ketan1248,
You can use our crypto or math classes to grab random numbers. Use that random number as an index for retrieval. Check here for more: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_crypto.htm and here: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_math.htm.
Convert to a list, then pull out a random element.
You really have two options.
The first option is to generate a random index, convert the set to a list, and fetch the list element using the bracket notation.
The second option is to generate the random index, then iterate through the Set until you get to the appropriate element.
Id randomid = mylist[math.random() * (mylist.size()-1)]