Drive

Google Fit Screenshot of 34 Heart Points and 5010 Steps

My car’s in the shop, so I had to take a Lyft to my dentist appointment.

Afterwards, I had a choice:

  • Hail another Lyft ride to get home (~$9 USD), or
  • Walk ~2 miles in 30-something °F to get my Heart Points/Steps for today

Sometimes the benefits of doing things the hard way outweighs the convenience of doing them the easy way. But you’ve got to pick and choose those opportunities wisely.

The Lucky Numbers Game and JS Simulations

Lately I’ve become low-key obsessed with The Lucky Numbers Game. You have a grid of numbers, ranging from 1 to 100, and “behind” one of those numbers is a coveted prize. The catch is the winning number is randomly assigned, and each guess costs as much as the number you’re selecting. So guessing 13 will cost you $13 (or chips, tokens, credits, what have you), and guessing 72 will cost 72.

The goal is to discover the winning lucky number in as few guesses/lowest cost as possible.

It’s a math puzzle? (sigh) “I’m in, you son of a bitch.”

There are three obvious approaches to take when confronting a lucky number grid:

  1. start at 1, and systematically work your way towards 100
  2. start at 100, and systematically work your way towards 1
  3. make 100 random guesses, hoping to get lucky and pick it earlier rather than later

So which approach helps us find the lucky number fastest? That’s where the JavaScript comes in. We can build a lo-fi version of the Lucky Number game fairly quickly, and test each approach thousands of times to figure out what the average number of guesses and costs for each approach is.

// gets a random number between min and max, inclusive. Source: Mozilla Developer Network
function getRandomIntInclusive(min, max) {
	min = Math.ceil(min);
	max = Math.floor(max);
	return Math.floor(Math.random() * (max - min + 1) + min);
}

// find lucky number, going from 1 to 100, in sequence
function runLowToHighTrial(){
	
	let guessHistory = [];
	let guessTokens = 0;
	let results = {};

	// randomly assign our lucky number
	let luckyNumber = getRandomIntInclusive(1,100);

	// make guesses from 1 to 100, until lucky number is found
	for (let x = 1; x <= 100; x++) {
		thisGuess = x;
		guessTokens = guessTokens + thisGuess;

		if (thisGuess == luckyNumber){
			break;
		} else {
			guessHistory.push(thisGuess);
		}
	}
	
	results.luckyNumber = luckyNumber;
	results.guessHistory = guessHistory;
	results.guessTokens = guessTokens;
	
	return results;
}

// find lucky number, going from 100 to 1, in sequence
function runHighToLowTrial(){
	
	let guessHistory = [];
	let guessTokens = 0;
	let results = {};

	// randomly assign our lucky number
	let luckyNumber = getRandomIntInclusive(1,100);

	// make guesses from 100 to 1, until lucky number is found
	for (let x = 100; x >= 1; x--) {
		thisGuess = x;
		guessTokens = guessTokens + thisGuess;

		if (thisGuess == luckyNumber){
			break;
		} else {
			guessHistory.push(thisGuess);
		}
	}
	
	results.luckyNumber = luckyNumber;
	results.guessHistory = guessHistory;
	results.guessTokens = guessTokens;
	
	return results;
}

// find lucky number, random guesses, remember/ignore previous guesses
function runRandomTrial(){
	
	let guessHistory = [];
	let guessTokens = 0;
	let results = {};

	// randomly assign our lucky number
	let luckyNumber = getRandomIntInclusive(1,100);

	// make one hundred random guesses, no duplicates
	for (let x = 1; x <= 100; x++) {
		do{
			thisGuess = getRandomIntInclusive(1,100);
		} while ( guessHistory.indexOf(thisGuess) > -1 );

		guessTokens = guessTokens + thisGuess;

		if (thisGuess == luckyNumber){
			break;
		} else {
			guessHistory.push(thisGuess);
		}
	}
	
	results.luckyNumber = luckyNumber;
	results.guessHistory = guessHistory;
	results.guessTokens = guessTokens;
	
	return results;
}

// calculate the average number of guesses and costs for each trial/approach
function getAverages(arrTrials){

	let totalGuesses = 0; 
	let totalTokens = 0; 
	let results = {};

	arrTrials.forEach( function(trial){ 
		totalGuesses = totalGuesses + trial.guessHistory.length;
		totalTokens = totalTokens + trial.guessTokens;
	});

	results.avgGuesses = totalGuesses / arrTrials.length;
	results.avgTokens = totalTokens / arrTrials.length;
	
	return results;
}

// we need to store our trial results
let lowToHighTrials = [];
let highToLowTrials = [];
let randomTrials = [];

// run 100K trials of each kind, storing results for averaging later
for (let a = 1; a <= 100000; a++){
	lowToHighTrials.push( runLowToHighTrial() );
	highToLowTrials.push( runHighToLowTrial() );
	randomTrials.push( runRandomTrial() );
}

// calculate averages, show results
console.log("Low To High Trials:");
console.log( getAverages(lowToHighTrials) );

console.log("High To Low Trials:");
console.log( getAverages(highToLowTrials) );

console.log("Random Trials:");
console.log( getAverages(randomTrials) );

I decided to run 100,000 trials of each approach, and this is what I found:

Low To High Trials: { average Guesses: 49.57338, average Cost: 1721.8413 }

High To Low Trials: { average Guesses: 49.41141, average Cost: 3379.74295 }

Random Trials: { average Guesses: 49.48288, average Cost: 2549.10839 }

Although no approach has an edge over the others in the fewest number of guesses, there’s definitely a difference in cost between them!

To be fair, we’re testing each approach in isolation– as if the Lucky Number game has only a single player. In reality, the Lucky Number game can have multiple simultaneous players, each competing to uncover the winning number before another player does. Players seem to start off with the Low To High approach initially, but change over to the Random Pick approach, possibly succumbing to the pressure of “What if one of my competitors finds the winning number in a higher range while I’m wasting my time down here in the lower ranges?”

It might be interesting to modify the JavaScript to have multiple players searching for the lucky number using the different approaches, to see how they fare against each other in direct competition. But not right now, I need to eat breakfast.

Funcertainty (sic) with WAVE

I had the surreal experience not too long ago of working with another developer and seeing radically different results through the WAVE Web Accessibility Evaluation Tool. While he saw only 2 errors, I saw over 300.

After comparing notes, we realized I was using the Chrome Extension while he was using the online version of the WAVE Web Accessibility Evaluation Tool. This wouldn’t normally matter, but the development server was using HTTP (port 80) instead of HTTPS (port 443). Since the online WAVE tool was using HTTPS, it was blocking out large portions of the content from the development server which contained issues.

What’s the takeaway?

  • If your development server still uses HTTP, you should upgrade to HTTPS
  • Use a WAVE browser extension instead of the online WAVE tool