Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                function factorial(n) {
	return n <= 1 ? 1 : n * factorial(n-1);
}

function combinations(n, k) {
	return factorial(n) / (factorial(k) * factorial(n - k));
}

function roll(n) {
	return Math.floor(Math.random() * n) + 1;
}

function highestNSum(amts, n) {
	/* returns sum of n highest dice */
	var sum = 0;
	var i = amts.length - 1;
	while (n > 0) {
		if (i < 0) {
			return -1;
		}
		sum += i * Math.min(n, amts[i]);
		n -= amts[i];
		--i;
	}
	return sum;
}

function incrementAmts(amts, buffer = 0) {
	/* goes to next dice combination */
	var i = amts.length;
	while(!amts[--i]){
		if (i === 0) {
			amts[0] = -1;
			return;
		}
	};
	
	if (i === amts.length - 1) {
		var buffer = amts[i];
		amts[i] = 0;
		incrementAmts(amts, buffer);
	}
	else {
		amts[i] -= 1;
		amts[i+1] += 1 + buffer;
	}
}

function amtsVariants(amts) {
	/* return amount of ways to throw a configuration */
	return factorial(amts.reduce((s, d) => s+d), 0) / (amts.reduce((s, d) => s * factorial(d), 1));
}

function averageRollsToScore(dice, score, turnTable) {
	if (score <= dice) {
		return 1;
	}
	if (score > dice * FACES) {
		return Infinity;
	}
	if (dice === 1) {
		return (FACES) / (FACES - score + 1);
	}
	
	if (turnTable[dice] && turnTable[dice][score]) {
		return turnTable[dice][score];
	}
	var amts = [0, dice];
	for (var i = 2; i <= FACES; ++i) {
		amts[i] = 0;
	}
	var avgTurns = [];
	while(amts[0] !== -1) {
		var best;
		if (highestNSum(amts, dice) >= score) {
			best = 1;
		}
		else {
			best = Infinity;
			for (var keep = 1; keep < dice; ++keep) {
				var newTurns = averageRollsToScore(dice-keep, score - highestNSum(amts, keep), turnTable);
				if (newTurns < best) {
					best = newTurns;
					if (best < 0) {
						debugger;
					}
				}
			}
			best += 1;
		}
		avgTurns.push({n: best, w: amtsVariants(amts)});
		incrementAmts(amts);
	}
	var result = findWeighedRepeatedAverage(avgTurns);
	if (typeof(result) === undefined) {
		debugger;
	}
	return result;
}

function findRepeatedAverage(numbers) {
	numbers = numbers.sort((a,b) => a-b);
	var keepAmt = Math.min(numbers.length, numbers.filter(a => a !== Infinity).length);
	while (keepAmt > 0) {
		//https://www.wolframalpha.com/input/?i=%28s%2Bk*%28x%2B1%29%29%2Fl+%3D+x%2C+solve+for+x
		var keepSum = numbers.slice(0, keepAmt).reduce((a, b) => a+b, 0);
		var rerollAmt = numbers.length - keepAmt;
		var x = (rerollAmt + keepSum) / (numbers.length - rerollAmt);
		if ((keepAmt === numbers.length || x+1 <= numbers[keepAmt]) && x+1 >= numbers[keepAmt - 1]) {
			return x;
		}
		--keepAmt;
	}
}
function findWeighedRepeatedAverage(numbers) {
	numbers = numbers.sort((a,b) => a.n-b.n);
	var totalWeight = numbers.reduce((s, a) => s + a.w, 0);
	var keepAmt = Math.min(totalWeight, numbers.filter(a => a.n !== Infinity).reduce((s, a) => s + a.w, 0));
	var i = Math.min(numbers.length, numbers.filter(a => a.n !== Infinity).length);
	while (i > 0) {
		var keepSum = numbers.slice(0, i).reduce((s, b) => s+b.n * b.w, 0);
		var rerollAmt = totalWeight - keepAmt;
		var x = (rerollAmt + keepSum) / (totalWeight - rerollAmt);
		if ((i === numbers.length || x+1 <= numbers[i].n) && x+1 >= numbers[i - 1].n) {
			return x;
		}
		keepAmt -= numbers[i-1].w;
		--i;
	}
}

function simpleGame(n) {
	var turns = 1;
	var dice = [];
	for (var i = 0; i < n; ++i) {
		dice.push(roll(FACES));
	}
	while (dice.filter(a => a !== FACES).length) {
		dice = dice.map(d => (d === FACES) ? d : roll(6));
		++turns;
	}
	return turns;
}

function simpleSim(n) {
	var s = 0;
	var numSims = 10000
	for (var i = 0; i < numSims; ++i) {
		s += simpleGame(n);
	}
	return s/numSims;
}

function createTable(avgTurns) {
	var $table = $("<table id='my-table'>");
	var $row = $("<tr>");
	for (var d = 0; d < avgTurns.length; ++d) {
		$row.append("<th>" + d + "</th>");
	}
	$table.append($row);
	for (var score = 1; score <= (avgTurns.length - 1) * FACES; ++score) {
		var $row = $("<tr>");
		$row.append("<th>" + score + "</th>");
		for (var d = 1; d < avgTurns.length; ++d) {
			$row.append("<td>" + ((avgTurns[d] && avgTurns[d][score]) ? (avgTurns[d][score]-1).toPrecision(5) : "-") + "</td>");
		}
		$table.append($row);
	}
	return $table;
}

var FACES;
function main() {
	//console.log(gamesAvgTurns(game2d, 100000));
	var dice = 6;
	FACES = 6;
	console.log("66 game avg turns: " + simpleSim());
	var avgTurns = [];
	for (var diceAmt = 1; diceAmt <= dice; ++diceAmt) {
		avgTurns[diceAmt] = [];
		for (var score = diceAmt; score <= diceAmt * FACES; ++score) {
			avgTurns[diceAmt][score] = averageRollsToScore(diceAmt, score, avgTurns);
		}
	}
	console.log(avgTurns);
	$("body").append(createTable(avgTurns));
}

onload = main;
              
            
!
999px

Console