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

@shaik.irfan 

Did you get a chance to read through my latest script which covers explanation as well.

Regards
Ankur

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

@Ankur Bawiskar 

 

Can you please help me with this line ?

 

 

  var rand = numRes[Math.floor(Math.random() * numRes.length)];

 

I know the functionality of Math.floor(Math.random() * length of numRes variable but why these everything is covered in numRes[] what does it do here 

Hi,

first you get the random number using this

Math.floor(Math.random() * numRes.length)

and then using numRes[] it determines which element to pick based on the index

Regards
Ankur

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

asifnoor
Kilo Patron

Here is how you can do. But do note that the if the no.of items fromw hich you have to pick random values is limited (like a,b,c,d,e), then the chances of getting same value is higher. So added a condition in the code.

tbl = "a,b,c,d,e";
var numRes = tbl.split(',');
var returnNum = 2;
var random = Math.floor((Math.random() * numRes.length));
var sys_id = numRes[random];
var sys_id1="";
while(true) {
  random1 = Math.floor((Math.random() * numRes.length));
  if(random1!=random) {
    sys_id1=numRes[random1];
    break;
  }
}

gs.print(sys_id+"---"+sys_id1)

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 answered your question, kindly mark my comment as a correct answer so that teh question is moved to the solved list.

Regards,
Asif
2020 ServiceNow Community MVP