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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

sample code below

var tbl = gs.getProperty('random.generator');
gs.print(tbl);
var numRes = tbl.split(',');

var val = getRandom(numRes,2);

gs.info('Random values are: ' + val);

function getRandom(arr, n) {
    var result = new Array(n),
        len = arr.length,
        taken = new Array(len);
    if (n > len)
               return 'getRandom: more elements taken than available';
    while (n--) {
        var x = Math.floor(Math.random() * len);
        result[n] = arr[x in taken ? taken[x] : x];
        taken[x] = --len in taken ? taken[len] : len;
    }
    return result;
}

 

Script:

var arr = ['a','b','c','d'];

var val = getRandom(arr,2);

gs.info(val);

function getRandom(arr, n) {
    var result = new Array(n),
        len = arr.length,
        taken = new Array(len);
    if (n > len)
       return 'getRandom: more elements taken than available';
    while (n--) {
        var x = Math.floor(Math.random() * len);
        result[n] = arr[x in taken ? taken[x] : x];
        taken[x] = --len in taken ? taken[len] : len;
    }
    return result;
}

 

Output:

find_real_file.png

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

Regards
Ankur

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

@Ankur Bawiskar 

 

Too difficult for me to understand if you dont mind can you please explain with the comments what you are doing ?

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

Hi Irfan,

Glad you got the solution. Kindly note that this code has chances of giving duplicates, where as the code that i gave you will always return unique elements. Just check once.