Within Salesforce if you would like to randomly select a record / object out of a list you would have to create something custom. This article will go through a randomizer class with several methods for how to randomly select a single record in a list, how to randomly select multiple records in list, and how to randomly select multiple records within different intervals in a list.
In order to create a random generator that will return a number from 0 – an input max number you can use the Math.Random method which returns 0 – 1 multiplied by the max input number:
Integer.valueOf(i*Math.random());//Math.random gets a random number from 0-1 multiplied by a number and you get a random number with the multiplied number as the Max value
From there you can use that number and use the size of the list minus 1 to get an index for a randomly selected item from a list:
public object getRandomRecord(List<Object> startingList){
Integer indexSizeOfList = startingList.size()-1;
Integer randomIndex = Integer.valueOf(indexSizeOfList*Math.random());//index starts at 0 size needs to be decreased then get the random number
return startingList[randomIndex];
}
By getting a random number multiple times and keeping track of those random numbers we can get a list of random numbers and use those as index numbers to get a list of random records:
public class Randomizer {
private List<Object> startingList;
private List<Object> placementList = new List<Object>();//list of randomized object records
public Randomizer(List<Object> listToRandomize){
this.startingList = listToRandomize;
}
public List<Object> getNumberOfRandomRecords(Integer i){
List<Integer> randomRecordIndexes = new List<Integer>();
for(Integer x = 0; x < i; x++){
Integer startingListMaxIndex = this.startingList.size() - 1;
Integer randomIndex = getRandomInteger(startingListMaxIndex);//get Integer
randomIndex = checkIntegerInList(randomRecordIndexes, randomIndex, startingListMaxIndex);//Check integer
placementList.add(startingList[randomIndex]);
randomRecordIndexes.add(randomIndex);
}
return placementList;
}
private Integer getRandomInteger(Integer i){
return Integer.valueOf(i*Math.random());//Math.random gets a random number from 0-1 multiplied by a number and you get a random number with the multiplied number as the Max value
}
private Integer checkIntegerInList(List<Integer> intList, Integer checkInt, Integer maxInt){ //checks to see if the integer is in the list, if not then returns the Integer, if it is then create a new one ***RECURSIVE***
if(intList.contains(checkInt)){
Integer newRandomInt = getRandomInteger(maxInt);
checkIntegerInList(intList, newRandomInt, maxInt);
}
return checkInt;
}
}
Taking the number of records you want to be returned divided by the length of the list will get you an interval that can be used. That way you can then get a random number of list records within each interval segment. Which would look something like this:
public class Randomizer {
private List<Object> startingList;
private List<Object> placementList = new List<Object>();//list of randomized object records
public Randomizer(List<Object> listToRandomize){
this.startingList = listToRandomize;
}
public List<Object> getNumberOfRandomRecordsWithinInterval(Integer i){
Decimal sizeOfList = startingList.size();
Integer intervalSize = Integer.valueOf((sizeOfList / Decimal.ValueOf(i)).round(System.RoundingMode.CEILING));//this is the celing but could also be the DOWN or in that case INT / INT gets the whole number after the division
for(Integer x = i;x-- > 0; ){
Integer randomIndex;
if(x != 0){
randomIndex = getRandomInteger(intervalSize) + (Integer.ValueOf(sizeOfList) - intervalSize*x);//segment
}else{
randomIndex = getRandomInteger(Integer.ValueOf(sizeOfList) - intervalSize*(i-1));//remaining segment
}
system.debug(randomIndex);
placementList.add(startingList[randomIndex]);
}
return placementList;
}
private Integer getRandomInteger(Integer i){
return Integer.valueOf(i*Math.random());//Math.random gets a random number from 0-1 multiplied by a number and you get a random number with the multiplied number as the Max value
}
}
Here is a link to the Github Repo I added in a few checks and made this a class that can get multiple randomized lists from the same list. This class has the ability to choose from several methods for how to randomly select a single record in a list, how to randomly select multiple records in list, and how to randomly select multiple records within different intervals in a list.
Checkout My New Course:
Crush It As A Salesforce Developer: Mastering Apex
Test Your Knowledge:
Try the new Salesforce Developer Quiz