Today I published my first Alexa Skill “Number Sequence Game”, a simple math game that asks the player to guess the next number in a given sequence. For examples, Fibonacci sequence 1, 3, 5, 8, the next number should be 13. The game includes 10 patterns so far and can generate more than 70 sequences randomly.

This is technically not my first Alexa Skill, it is the fifth I ever built, the rest are for Hackathons and my personal Echos.

Building an Alexa Skill is very easy, way much easier than a mobile app or a web app. Once we understand the Alexa utterance model and write some Lambda function, we are pretty much all set. In order to build a good Skill, we need to design the user interaction with Voice First mindset: it is a Skill, not an app, therefore, focus on the user experiences over voice.

The certification process is similar to iOS app – a manual process, which takes about 3-5 business days. Note that even though the Skill is for everyone including children, we should not mention that it is directed at children, see the content policy.

One of the challenges – yes, this 300-line program has challenges – is that I need to generate a weighted random number, which will be used to determine which type of sequence I want to compute. Let’s take a look at the sequence data definition first:

const PATTERNS = [ { "name": "ARITHMETIC", "length": 4, "operation": "add", "isDynamic": true, "values": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], "firstValueLow": 1, "firstValueHigh": 5, "weight": 5 }, { "name": "FIBONACCI", "length": 6, "operation": "", "isDymaic": false, "values": [1, 1, 2, 3, 5, 8, 13] }, ...... ]

I have two types of patterns: a) Dynamic, such as Arithmetic, can be generated dynamically and can have different starting number; b) Static, such as Fibonacci, is a fixed sequence and should always start from 1 or the first logical number. I want to add more weights to dynamic patterns.

The solution is to use a lookup table that duplicates the item with its weight. For examples, [A, B], if A’s weight is 2 while B’s weight is 1, we need to transform [A, B] to [A, A, B], and then generate a random number between 1 and 3. If the number is 1 or 2, then A, if 3, then B. Here is the Javascript version of the algorithm:

function getRandomPattern() { // Apply weight to randomization var lookupTable = []; for (var i = 0; i < PATTERNS.length; i++) { var weight = PATTERNS[i].weight; if (weight) { for (var j = 0; j < weight; j++) { lookupTable.push(i); } } else { lookupTable.push(i); } } var rand = randomInt(0, lookupTable.length - 1); const randomPattern = PATTERNS[lookupTable[rand]]; return randomPattern; } function randomInt(low, high) { return Math.floor(Math.random() * (high - low + 1) + low); }

Creating an Alexa Skill is fun, easy, and low cost (no need to pay developer license, but AWS Lambda usage). Starting building one today!