had a request from a client recently to generate a random list of records from a given table (specifically the Configuration Item table). These items would be used as a pool of records for a random audit of the records in that table. I don’t think this will be used all that often but I figured I’d throw it out here so that people could use it if they needed it. Here’s the code…

Return Random Records Script
This script is set up to return an array containing the names of 5 randomly-selected records from the ‘cmdb_ci’ table. You can set the ‘tbl’ and ‘returnNum’ variables to customize the number of records and the table to query from. You may also choose to modify the GlideRecord query to limit the scope of the query.

var tbl = 'cmdb_ci'; //Query from this table
var returnNum = 5; //Return this number of records in the array
var answer = new Array();

//Set up the GlideRecord query
var rec = new GlideRecord(tbl);
   //Get the number of records queried
   var numRecs = rec.getRowCount();
   //Ensure number of records is not less than number to return
   if(numRecs < returnNum){
     returnNum = numRecs;
   //Return 'returnNum' number of random records in an array
   for(i=0; i < returnNum; i++){
      //Get a random number between 0 and 'numRecs'
      var randomNumber = Math.floor(Math.random()*numRecs);
      //Set the row number to 'randomNumber'
      //Add the random record to the array
gs.log(answer.join()); //Log the output
return answer; //Return the array