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
vijendhar k 23vijendhar k 23 

Random list of ID's Generation

Hello all,
   How to generate random list of ID's from existed list.
example : i have 1000 records in list. in that i have to generate 10 random records. and the 10 random records must be unique
how to achive this functionallity using APEX.
Its bit urgent requirement . your help is appreciable.

Thanks
Vijay
 
ArvindyadavArvindyadav
Hi Vijendhar,

please try the below logic if it work for you.

For(i=0;i<10;i++)
{
static Random rnd = new Random();
int r = rnd.Next(yourlist.Count);
if(newlist.contains(yourlist.get(r))){
i--;
}else{
newlist.add(yourlist.get(r));
}
}