RemcoE33

Randome unique value.

Jul 25th, 2021 (edited)
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Return randome unique values from a array
  3.  *
  4.  * @param {A1:D10} data - Your dataset.
  5.  * @param {5} amount  - number of values returned.
  6.  * @return Unique values from input
  7.  * @customfunction
  8.  */
  9. function RNDUNIQ(data, amount) {
  10.   const arr1d = [].concat(...data);
  11.   const result = [];
  12.   const numbers = [];
  13.   const arrLength = arr1d.length;
  14.  
  15.   do {
  16.     const n = rand(arrLength);
  17.     if (numbers.indexOf(n) < 0){
  18.       numbers.push(n); }
  19.   } while (numbers.length < amount)
  20.    
  21.     for ( var i = 0; i < amount; i++ ) {
  22.       result.push(arr1d[numbers[i]]);
  23.     }
  24.  
  25.   return result;
  26.  
  27. }
  28.  
  29. function rand(length) {
  30.   return Math.floor(Math.random() * length);
  31. }
Add Comment
Please, Sign In to add comment