index.js
function selectWithoutValidation(choices) {
	// Generate a list of options with positive non-zero chances
	let choicesWithNonZeroChances = [];
	let totalWeight = 0.0;
	for(let i = 0; i < choices.length; i++) {
		if(choices[i].chance > 0.0) {
			choicesWithNonZeroChances.push(choices[i]);
			totalWeight += choices[i].chance;
		}
	}

	// Pick a random value from [0,1)
	let value = Math.random() * totalWeight;

	// Iterate over possibilities until we find the selected one
	let chanceCovered = 0.0;
	for(let i = 0; i < choicesWithNonZeroChances.length; i++) {
		chanceCovered += choicesWithNonZeroChances[i].chance;
		if(value < chanceCovered) {
			return choicesWithNonZeroChances[i].result;
		}
	}
}
function selectWithoutValidation(choices) {
	// Generate a list of options with positive non-zero chances
	let choicesWithNonZeroChances = [];
	let totalWeight = 0.0;
	for(let i = 0; i < choices.length; i++) {
		if(choices[i].chance > 0.0) {
			choicesWithNonZeroChances.push(choices[i]);
			totalWeight += choices[i].chance;
		}
	}

	// Pick a random value from [0,1)
	let value = Math.random() * totalWeight;

	// Iterate over possibilities until we find the selected one
	let chanceCovered = 0.0;
	for(let i = 0; i < choicesWithNonZeroChances.length; i++) {
		chanceCovered += choicesWithNonZeroChances[i].chance;
		if(value < chanceCovered) {
			return choicesWithNonZeroChances[i].result;
		}
	}
}
function selectWithoutValidation ( choices ) { // Generate a list of options with positive non-zero chances let choicesWithNonZeroChances = [ ] ; let totalWeight = 0.0 ; for ( let i = 0 ; i < choices . length ; i ++ ) { if ( choices [ i ] . chance > 0.0 ) { choicesWithNonZeroChances . push ( choices [ i ] ) ; totalWeight += choices [ i ] . chance ; } } // Pick a random value from [0,1) let value = Math . random ( ) * totalWeight ; // Iterate over possibilities until we find the selected one let chanceCovered = 0.0 ; for ( let i = 0 ; i < choicesWithNonZeroChances . length ; i ++ ) { chanceCovered += choicesWithNonZeroChances [ i ] . chance ; if ( value < chanceCovered ) { return choicesWithNonZeroChances [ i ] . result ; } } }
Performs the random selection without validating any input
Performs the random selection without validating any input