German Luck

Germany's chances to get the perfect group for the volleyball Olympic qualifications

by Noncho Savov

HTML

All volleyball teams and their world ranking:
<table style="margin-top:5px">
<tr>
<td>Serbia [9]</td>
<td>France [11]</td>
</tr>
<tr>
<td>Belgium [13]</td>
<td>Bulgaria [14]</td>
</tr>
<tr>
<td>Netherlands [15]</td>
<td>Slovenia [17]</td>
</tr>
<tr>
<td>Germany [27]</td>
<td>Czech [30]</td>
</tr>
</table>
<p id="txt"></p>
<button onclick="pause()">Pause</button>
<button onclick="next()" style="display:none" id="next">Next</button>
<br/><br/><span style="color:#666;">
  What makes a group perfect for Germany:
  <br/>1. The two highest ranked teams (Sebia and France) must NOT be cast in the same group as Germany.
  <br/>2. Three of the lowest ranked teams (including Germany) must be cast in one same group.
  <br/>3. Only the combination of the above two criteria guarantees the easiest possible way to the semi-finals.
  <br/><br/>Note: This calculator relies only on the official ranking numbers and does not make any assumptions if one team is better than the other.
</span>

CSS

table, th, td {
  border: 1px solid black;
  padding:5px;
}

JavaScript

var indexes = [9, 11, 13, 14, 15, 17, 27, 30];
var arr, arrA, arrB;
var positive = 0;
var negative = 0;
var pos;
var paused;

requestAnimationFrame(calculate);

//Fisher-Yates (aka Knuth) unbiased shuffle
//https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
function shuffle(array) {
	var currentIndex = array.length, temporaryValue, randomIndex;
	while (0 !== currentIndex) {
		randomIndex = Math.floor(Math.random() * currentIndex);
		currentIndex -= 1;
		temporaryValue = array[currentIndex];
		array[currentIndex] = array[randomIndex];
		array[randomIndex] = temporaryValue;
	}
	return array;
}

function pause(){
	paused = !paused;
  document.getElementById("next").style.display = (paused ? "inline" : "none");
}

function calculate(){
	if(!paused) next();
  requestAnimationFrame(calculate);
}

function next(){
	arr = shuffle(indexes);
    arrA = [];
    arrB = [];
    arrA.push(arr[0]);arrA.push(arr[1]);arrA.push(arr[2]);arrA.push(arr[3]);
    arrB.push(arr[4]);arrB.push(arr[5]);arrB.push(arr[6]);arrB.push(arr[7]);
    if(
      (
        (arrA.indexOf(9)>-1 && arrA.indexOf(11)>-1 && arrA.indexOf(27)==-1) ||
        (arrB.indexOf(9)>-1 && arrB.indexOf(11)>-1 && arrB.indexOf(27)==-1)
      ) &&
      (
        (arrA.indexOf(30)>-1 && arrA.indexOf(27)>-1 && arrA.indexOf(17)>-1) ||
        (arrB.indexOf(30)>-1 && arrB.indexOf(27)>-1 && arrB.indexOf(17)>-1)
      )
    ){
      pos = true;
      positive ++;
    } else {
      pos = false;
      negative ++;
    }
    document.getElementById("txt").innerHTML = "Tries:"+(positive+negative)+
    "<br/>positive:"+positive+(pos?"+":"")+
    "<br/>negative:"+negative+(pos?"":"+")+
    "<br/><br/>Randomized cast:"+
    "<br/>Group A: [" +arrA + "];<br/>Group B: [" + arrB + "];"+
    "<br/>Germany wast cast in group "+(arrA.indexOf(27)>-1?"A":"B")+
    "<br/>Is the group perfect: "+(pos?"Yes":"No")+
    "<br/><br/>" +
    "<b>Chance: " + ((positive / (positive+negative))*100).toFixed(2) +...