Skip to content

Instantly share code, notes, and snippets.

@nootanghimire
Created May 14, 2016 08:55
Show Gist options
  • Save nootanghimire/e0c140463a9cf551725d0f184c136e83 to your computer and use it in GitHub Desktop.
Save nootanghimire/e0c140463a9cf551725d0f184c136e83 to your computer and use it in GitHub Desktop.
A Code Specific to an example to generate permuted block randomization for sampling
function generateRandom(_threshold){
var threshold = _threshold || 0.5;
returnArray = [];
holdArray = [];
for(var i = 1; i<=200; i++) {
holdArray = []; //init
for(var j = 1; j<=6; j++) {
if(Math.random() >= threshold){
if(howmanyInArray(holdArray, "odd") >= 3) {
holdArray.push("even");
} else {
holdArray.push("odd");
}
} else {
if(howmanyInArray(holdArray, "even") >= 3) {
holdArray.push("odd");
} else {
holdArray.push("even");
}
}
}
returnArray.push(holdArray);
}
return returnArray;
}
function howmanyInArray(arr, num){
var count = 0;
for (var i = arr.length - 1; i >= 0; i--) {
if(arr[i] == num){
count++;
}
};
return count;
}
function writeCSVFrom2DArray(arr){
//Assume array is 2d
//
for (var i = arr.length - 1; i >= 0; i--) {
document.write(arr[i].join(","));
document.write("\n");
};
}
var arr = generateRandom();
writeCSVFrom2DArray(arr);
/** View Source and copy to a text file
renaming to to <something>.csv
-- Open with Spreadsheet program
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment