Need help on Random Selection of value

shaik_irfan
Tera Guru

Hi,

 

I have a system property from where i wanted to pick only 2 random values every time.

 

I am able to pick one value but can anyone please help me how to pick 2 random values every time ?

 

var tbl = gs.getProperty('random.generator');
gs.print(tbl);
var numRes = tbl.split(',');
var returnNum = 2;
var random = Math.floor((Math.random() * numRes.length));
var sys_id = numRes[random];
gs.print(sys_id)

 

Ouput:

*** Script: a,b,c,d,e
*** Script: d


But i wanted to display 2 values like a,b or b,d or a,c

 

1 ACCEPTED SOLUTION

Hi Shaik,

simpler code and explanation:

var finalArray = randomElements();

gs.info('finalArray is:' + finalArray);

function randomElements() {
  var finalArr = [];
 
  var tbl = gs.getProperty('random.generator');
  var numRes = tbl.split(',');
  var returnNum = 2; // give here how many random elements you want
 
  if(returnNum > numRes.length)
   return 'more elements taken than available';

  for (var i = 0; i < returnNum; i++) {
    var rand = numRes[Math.floor(Math.random() * numRes.length)];
    finalArr.push(rand.toString());
  }
  return finalArr.join(",");
}

Explanation:

It first checks if number of random elements asked is less than array length

1) since you want 2 random elements the for loop runs for 2 times

2) each time it generates a random number and then uses that as index to determine the array element

3) it then pushes that value in final array and then returns that final array

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

12 REPLIES 12

@asifnoor 

 

It works for me but just for my understanding i mention as 2. but in real client instance we have bunch of data and we need to pick 20 values randomly so i little confused do i need to write math.floor method 20 times ?

Whether its 2 or 20, you need to know how many random values you have to generate form the given list and obviously the number of random items that you can choose should be less than the total list. If the no. of random items that you need to pick is more and nearer to the complete list count, then the script will take lot of time because its difficult to pick the random item from the list.

Here is how you can try.

tbl = "a,b,c,d,e,f,g,h,i,j,k,lm,n,o,p";
var numRes = tbl.split(',');
var returnNum = 3;
var random_numbers=[];
for(i=0;i<returnNum;i++) {
var random = Math.floor((Math.random() * numRes.length));
  if(i==0) {
     random_numbers.push(numRes[random]);
  } else {
      random = Math.floor((Math.random() * numRes.length));
      while(true) {         
         if(random_numbers.indexOf(numRes[random]) == -1) {
            random_numbers.push(numRes[random]);
            break;
         }
       }      
  }
}
gs.print(random_numbers);

To test, ensure the random values count is 30% of the total list count, that way your script will be faster. 

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

If this has resolved your problem, kindly mark the comment as a correct answer.