Here a sample survey
As I said I use the plugin "getStatsInSurvey"
To count the 36 possible groups I create a question of type "list(radio)" ("QGA"), which is hidden.
Next I created a question of type array(text) ("QGB") with 9 rows (all combinations of age and gender) and 4 columns.
Here the plugin is called.
It catptures the values of all 36 outcomes and saves it into the cells of the array.
Code:
<script type="text/javascript" charset="utf-8">
$(document).on('ready pjax:scriptcomplete',function(){
var total1=Math.floor({' [QGA.nb.1]'});
var total2=Math.floor({' [QGA.nb.2]'});
var total3=Math.floor({' [QGA.nb.3]'});
var total4=Math.floor({' [QGA.nb.4]'});
var total5=Math.floor({' [QGA.nb.5]'});
var total6=Math.floor({' [QGA.nb.6]'});
var total7=Math.floor({' [QGA.nb.7]'});
var total8=Math.floor({' [QGA.nb.8]'});
var total9=Math.floor({' [QGA.nb.9]'});
var total10=Math.floor({' [QGA.nb.10]'});
var total11=Math.floor({' [QGA.nb.11]'});
var total12=Math.floor({' [QGA.nb.12]'});
var total13=Math.floor({' [QGA.nb.13]'});
var total14=Math.floor({' [QGA.nb.14]'});
var total15=Math.floor({' [QGA.nb.15]'});
var total16=Math.floor({' [QGA.nb.16]'});
var total17=Math.floor({' [QGA.nb.17]'});
var total18=Math.floor({' [QGA.nb.18]'});
var total19=Math.floor({' [QGA.nb.19]'});
var total20=Math.floor({' [QGA.nb.20]'});
var total21=Math.floor({' [QGA.nb.21]'});
var total22=Math.floor({' [QGA.nb.22]'});
var total23=Math.floor({' [QGA.nb.23]'});
var total24=Math.floor({' [QGA.nb.24]'});
var total25=Math.floor({' [QGA.nb.25]'});
var total26=Math.floor({' [QGA.nb.26]'});
var total27=Math.floor({' [QGA.nb.27]'});
var total28=Math.floor({' [QGA.nb.28]'});
var total29=Math.floor({' [QGA.nb.29]'});
var total30=Math.floor({' [QGA.nb.30]'});
var total31=Math.floor({' [QGA.nb.31]'});
var total32=Math.floor({' [QGA.nb.32]'});
var total33=Math.floor({' [QGA.nb.33]'});
var total34=Math.floor({' [QGA.nb.34]'});
var total35=Math.floor({' [QGA.nb.35]'});
var total36=Math.floor({' [QGA.nb.36]'});
$('#answer{SGQ}Y001_X001').val(total1);
$('#answer{SGQ}Y002_X001').val(total2);
$('#answer{SGQ}Y003_X001').val(total3);
$('#answer{SGQ}Y004_X001').val(total4);
$('#answer{SGQ}Y005_X001').val(total5);
$('#answer{SGQ}Y006_X001').val(total6);
$('#answer{SGQ}Y007_X001').val(total7);
$('#answer{SGQ}Y008_X001').val(total8);
$('#answer{SGQ}Y009_X001').val(total9);
$('#answer{SGQ}Y001_X002').val(total10);
$('#answer{SGQ}Y002_X002').val(total11);
$('#answer{SGQ}Y003_X002').val(total12);
$('#answer{SGQ}Y004_X002').val(total13);
$('#answer{SGQ}Y005_X002').val(total14);
$('#answer{SGQ}Y006_X002').val(total15);
$('#answer{SGQ}Y007_X002').val(total16);
$('#answer{SGQ}Y008_X002').val(total17);
$('#answer{SGQ}Y009_X002').val(total18);
$('#answer{SGQ}Y001_X003').val(total19);
$('#answer{SGQ}Y002_X003').val(total20);
$('#answer{SGQ}Y003_X003').val(total21);
$('#answer{SGQ}Y004_X003').val(total22);
$('#answer{SGQ}Y005_X003').val(total23);
$('#answer{SGQ}Y006_X003').val(total24);
$('#answer{SGQ}Y007_X003').val(total25);
$('#answer{SGQ}Y008_X003').val(total26);
$('#answer{SGQ}Y009_X003').val(total27);
$('#answer{SGQ}Y001_X004').val(total28);
$('#answer{SGQ}Y002_X004').val(total29);
$('#answer{SGQ}Y003_X004').val(total30);
$('#answer{SGQ}Y004_X004').val(total31);
$('#answer{SGQ}Y005_X004').val(total32);
$('#answer{SGQ}Y006_X004').val(total33);
$('#answer{SGQ}Y007_X004').val(total34);
$('#answer{SGQ}Y008_X004').val(total35);
$('#answer{SGQ}Y009_X004').val(total36);
// $('#question{QID}').hide();
});
</script>
There will be a shorter solution, but I am not able to do it.
Now we see this overview (of course also hidden by uncommenting the line "// $('#question{QID}').hide();")
Let's concentrate on the red marked rows.
You see
1. the next selected group of "mid-female" should be Group 1
2. the next selected group of "mid-male" should be one of Group 1, 2 or 4.
Okay, next.
By the values of age and gender you can calculate the row
{(3*(age-1)+gender)}
This is done in the first equation "eqGroup"
Now you find the minimum value in this row by (do not copy this without removing the line feeds)
{
if(eqGroup==1,min(that.QGB.sq_Y001.NAOK),
if(eqGroup==2,min(that.QGB.sq_Y002.NAOK),
if(eqGroup=
,min(that.QGB.sq_Y003.NAOK),
if(eqGroup==4,min(that.QGB.sq_Y004.NAOK),
if(eqGroup==5,min(that.QGB.sq_Y005.NAOK),
if(eqGroup==6,min(that.QGB.sq_Y006.NAOK),
if(eqGroup==7,min(that.QGB.sq_Y007.NAOK),
if(eqGroup==8,min(that.QGB.sq_Y008.NAOK),
min(that.QGB.sq_Y009.NAOK)
)
)
)
)
)
)
)
)
}
This is done in equation "eqMinVal".
Now you know the minimum value, but still you don't know which group has this value.
In the next equation
{
if(eqGroup==1,join(if(QGB_Y001_X001==eqMinVal,"1",""),if(QGB_Y001_X002==eqMinVal,"2",""),if(QGB_Y001_X003==eqMinVal,"3",""),if(QGB_Y001_X004==eqMinVal,"4","")),
if(eqGroup==2,join(if(QGB_Y002_X001==eqMinVal,"1",""),if(QGB_Y002_X002==eqMinVal,"2",""),if(QGB_Y002_X003==eqMinVal,"3",""),if(QGB_Y002_X004==eqMinVal,"4","")),
if(eqGroup=
,join(if(QGB_Y003_X001==eqMinVal,"1",""),if(QGB_Y003_X002==eqMinVal,"2",""),if(QGB_Y003_X003==eqMinVal,"3",""),if(QGB_Y003_X004==eqMinVal,"4","")),
if(eqGroup==4,join(if(QGB_Y004_X001==eqMinVal,"1",""),if(QGB_Y004_X002==eqMinVal,"2",""),if(QGB_Y004_X003==eqMinVal,"3",""),if(QGB_Y004_X004==eqMinVal,"4","")),
if(eqGroup==5,join(if(QGB_Y005_X001==eqMinVal,"1",""),if(QGB_Y005_X002==eqMinVal,"2",""),if(QGB_Y005_X003==eqMinVal,"3",""),if(QGB_Y005_X004==eqMinVal,"4","")),
if(eqGroup==6,join(if(QGB_Y006_X001==eqMinVal,"1",""),if(QGB_Y006_X002==eqMinVal,"2",""),if(QGB_Y006_X003==eqMinVal,"3",""),if(QGB_Y006_X004==eqMinVal,"4","")),
if(eqGroup==7,join(if(QGB_Y007_X001==eqMinVal,"1",""),if(QGB_Y007_X002==eqMinVal,"2",""),if(QGB_Y007_X003==eqMinVal,"3",""),if(QGB_Y007_X004==eqMinVal,"4","")),
if(eqGroup==8,join(if(QGB_Y008_X001==eqMinVal,"1",""),if(QGB_Y008_X002==eqMinVal,"2",""),if(QGB_Y008_X003==eqMinVal,"3",""),if(QGB_Y008_X004==eqMinVal,"4","")),
join(if(QGB_Y009_X001==eqMinVal,"1",""),if(QGB_Y009_X002==eqMinVal,"2",""),if(QGB_Y009_X003==eqMinVal,"3",""),if(QGB_Y009_X004==eqMinVal,"4",""))
)
)
)
)
)
)
)
)
}
you join all group numbers that contain this minimum value.
Done in equation "eqMinGroup"
Now you generate a random number from 1 to the length of the joined string.
{if(is_empty(eqRandGroup),rand(1,strlen(eqMinGroup))-1,eqRandGroup)}
Done in equation "eqRandGroup"
The last step is to set this found group number in the list(radio)
{QGA=9*(sum(eqEndGroup,-1))+eqGroup}
Let's see it live.
I selected "mid-male" and the row is calculated as 4th.
Here the minimum is "0" in columns 1, 2 and 4.
Here you see the result of the equations. In a real survey these are hidden, of course.
And here just an info page that shows the result.
BUT:
This only works really fine, if there are not too many participants answering the survey at the same time.
Only completed surveys are taken in account.
So imagine:
At the start all cells are empty.
If 50 young males start the survey at the same time they are distributed at random to the four groups.
This way there is no advantage to a pure random distribution.
To avoid this you should send out the invitations in batches spread over some time.
Joffm