Skip to content

Instantly share code, notes, and snippets.

@remie
Last active August 29, 2015 14:17
Show Gist options
  • Save remie/989617cc4eb0c3dd31ef to your computer and use it in GitHub Desktop.
Save remie/989617cc4eb0c3dd31ef to your computer and use it in GitHub Desktop.
Randomized Controlled Trial (RCT) research number generator in JavaScript
<!--
================================================================================================
Randomized Controlled Trial (RCT) research number generator in JavaScript
Generates a set of research numbers equally devided between two groups
(intervention and control group). Uses block randomization with variable
block sizes, random permutation loosly based on Knuth shuffle and random
group type selection.
Depends on Math.Random() to generate the random numbers.
Keep in mind that this is a Pseudo Random Number Generator (PRNG) of
which the implementations vary per browser.
Copyright 2015, R. Bolte / N.D.L. Hallensleben
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public version 3 License as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
================================================================================================
-->
<html>
<head>
<script src="http://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
// Variable initialization
var arrA=[], arrB=[];
// block randomization with random block size
while(true) {
var x = arrA.length + arrB.length + 1;
var blocksize = Math.floor(Math.random() * (10 - 4 + 1)) + 4;
if(x > 100) { break; }
var evens=[], odds=[];
for(var i=x;i < x + blocksize;i++) {
var PRNGResult = Math.floor((Math.random() * 100) + 1);
if(PRNGResult % 2 == 0) { evens.push(("00" + i).slice(-3)); }
else { odds.push(("00" + i).slice(-3)); }
}
if(arrA.length < arrB.length) {
arrA = $.merge((evens.length > odds.length) ? evens : odds, arrA);
arrB = $.merge((evens.length > odds.length) ? odds : evens, arrB);
} else {
arrB = $.merge((evens.length > odds.length) ? evens : odds, arrB);
arrA = $.merge((evens.length > odds.length) ? odds : evens, arrA);
}
}
// Random Permutations (loosly based on Knuth shuffle)
var shuffles = Math.floor(Math.random() * (75 - 25 + 1)) + 25;
for(var i=0;i<shuffles;i++) {
var posA = Math.floor((Math.random() * 100) + 1),
posB = Math.floor((Math.random() * 100) + 1);
var swap = arrA[posA];
arrA[posA] = arrB[posB];
arrB[posB] = swap;
}
// Bucket type randomization
var rndTypeChooser = Math.floor((Math.random() * 100) + 1);
var typeA = (rndTypeChooser % 2 == 0) ? "Intervention" : "Control";
var typeB = (rndTypeChooser % 2 == 0) ? "Control" : "Intervention";
// Type assignment
var result = {};
var countA=0, countB=0;
for(var i=1;i <= 100;i++) {
var current = ("00" + i).slice(-3);
result[current] = ($.inArray(current, arrA) >= 0) ? typeA : typeB;
if(result[current] == typeA) { countA++; } else { countB++ }
}
// Display results
var keys = [];
$.each(result, function(key, type) { keys.push(key); });
keys.sort();
$('#result').append('number,type</br>');
$.each(keys, function(index, key) {
$('#result').append('"' + key + '","' + result[key] + '"</br>');
});
$('#result').append('<br />' + typeA + ': ' + countA + '<br />' + typeB + ': ' + countB);
});
</script>
</head>
<body>
<div id="result"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment