function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AmberTaylorAmberTaylor 

Get a random number that change anytime a function is called

Hello, 

I have tried to generate a random number that changes any time my function is called using Math.Random. but it appears that the number is still the same in the context, and never change : 
here is what i've done. 

The function : 
public static Integer getRandomNumber(Integer listSize){
        Integer randomNumber = Integer.valueof(Math.random() * listSize);
        System.debug('randomNumber' + randomNumber);
        return randomNumber;
    }

When i call the previous function :
List<trailheadapp__User_Badge__c> recordsToInsert = new List<trailheadapp__User_Badge__c>();
        for(Integer i=0; i<300; i++) {
            String randomString = badgeStatus[getRandomNumber(listSize)];
            String randomBadge = badgeList[getRandomNumberForBadge(listBadgeSize)];
            String randomUserID = idList[getRandomId(listIdSize)];
            System.debug('RANDOM USER ID = '+randomUserID);
            trailheadapp__User_Badge__c newJunctionRecord = new trailheadapp__User_Badge__c (trailheadapp__External_ID__c = '0050E00000823JEQAY-a093X00001SmGRWQA'+i,trailheadapp__Status__c = randomString, trailheadapp__Badge__c = randomBadge, trailheadapp__User__c = randomUserID);
            recordsToInsert.add(newJunctionRecord);
        }
        insert recordsToInsert;
The random number remains the sames for all records inserted, what i want is that number changes for every record. 

Does someone see something wrong ?

Ty in advance for help
Amber
ANUTEJANUTEJ (Salesforce Developers) 
Hi Amber,

Can you elaborate the use case or the issue so that we can check further and respond back?

Looking forward to your response.

Thanks.
AmberTaylorAmberTaylor
Hi Anutej, 

In a test class i need to create a bunch of records of trailheadapp__User_Badge__c SObject (badges object), that are linked to a user randomly. There are 7 users (previously created) and i need this number to go from 1 to 7 each time a record is added to my list that i'll insert to create all badges record once my loop is processed. 

The 'error' I encounter is that my random number is generated at the beginning and never changes afterwards, that mean all my created trailheadapp__User_Badge__c SObject are always linked to the same user and it never change. Other users never have attributed badges randomly.
 
List<trailheadapp__User_Badge__c> recordsToInsert = new List<trailheadapp__User_Badge__c>(); 
        for(Integer i=0; i<300; i++) { // loop that is supposed to attribuate randomly users to my badges records
            String randomString = badgeStatus[getRandomNumber(listSize)];
            String randomBadge = badgeList[getRandomNumberForBadge(listBadgeSize)];
            String randomUserID = idList[getRandomId(listIdSize)];
            System.debug('RANDOM USER ID = '+randomUserID);
            trailheadapp__User_Badge__c newJunctionRecord = new trailheadapp__User_Badge__c (trailheadapp__External_ID__c = '0050E00000823JEQAY-a093X00001SmGRWQA'+i,trailheadapp__Status__c = randomString, trailheadapp__Badge__c = randomBadge, trailheadapp__User__c = randomUserID);
            recordsToInsert.add(newJunctionRecord);
        }
        insert recordsToInsert;

What i need to do in my loop that add records to my list, is each time my loop restart this variable changes : String randomUserID = idList[getRandomId(listIdSize)]; but it never changes in the context. 
Maybe i need to use something else instead of Math.Random? I don't know.

TY in advance