Created
February 16, 2016 11:48
-
-
Save karlpokus/d73937d9dad2ea90c4e9 to your computer and use it in GitHub Desktop.
Randomize dataset by randomList of unique random values for GAS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function randomList(range, n) { | |
// | |
var checklist = [], out = []; | |
// loop | |
for (var i = 0; out.length < n; i++) { | |
// | |
var num = Math.floor((Math.random() * range)); | |
// if not in checklist | |
if (checklist.indexOf(num) === -1) { | |
// | |
out.push(num); | |
// | |
checklist.push(num); | |
} | |
} | |
// | |
return out; | |
} | |
function randomize(data, n) { | |
// out | |
var out = [], row, num; | |
// random numbers | |
var list = randomList(data.length, n); | |
// loop | |
for (var i = 0; i < list.length; i++) { | |
// | |
num = list[i]; | |
row = data[num]; | |
// | |
out.push(row); | |
} | |
// | |
return out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment