Skip to content

Instantly share code, notes, and snippets.

@Carpetfizz
Created January 15, 2015 02:50
Show Gist options
  • Save Carpetfizz/0495582968ccecd35d0b to your computer and use it in GitHub Desktop.
Save Carpetfizz/0495582968ccecd35d0b to your computer and use it in GitHub Desktop.
Experimental Design Random Name Picker
var fs = require('fs');
var data = fs.readFileSync('listofnames.txt','utf8').toString().split("\n");
var mainset = []; //Random group of 60 students
var subsetOne = []; //Random group of a subset of 30 / 60 students
var subsetTwo = []; //Random group of a subset of 30 / 60 students
var indicesSetOne = [];
var indicesSetTwo = [];
var indicesSetThree = [];
//Random function with arg max number
function r(max){
return Math.floor(Math.random() * (max - 0) + 0);
}
/* Get 60 random numbers in between 0 and 74) */
while(indicesSetOne.length < 60){
var num = r(75);
if(indicesSetOne.indexOf(num) == -1){
indicesSetOne.push(num);
}
}
/* Slurp names into mainset */
for(var i in indicesSetOne){
mainset.push(data[indicesSetOne[i]]);
}
/* Get 30 random numbers in between 0 and 59 */
while(indicesSetTwo.length < 30) {
var num = r(60);
if(indicesSetTwo.indexOf(num) == -1){
indicesSetTwo.push(num);
}
}
/* Slurp names into subsetOne */
for(var i in indicesSetTwo){
subsetOne.push(data[indicesSetTwo[i]]);
}
/* Get 30 random numbers in between 0 and 59
but make sure they haven't already been chosen
*/
while (indicesSetThree.length < 30){
var num = r(60);
if(indicesSetThree.indexOf(num) == -1){
if(indicesSetTwo.indexOf(num) == -1){
indicesSetThree.push(num);
}
}
}
/* Slurp names into subsetTWo */
for (var i in indicesSetThree){
subsetTwo.push(data[indicesSetThree[i]]);
}
/* Write output file */
fs.writeFile('output.txt',"===First Group of 30==="+"\n"+subsetOne+"\n"+"===Second Group of 30==="+"\n"+subsetTwo);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment