Skip to content

Instantly share code, notes, and snippets.

@michaellunzer
Created March 31, 2021 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaellunzer/11e9d70b8ba07de971ae7e414542ff00 to your computer and use it in GitHub Desktop.
Save michaellunzer/11e9d70b8ba07de971ae7e414542ff00 to your computer and use it in GitHub Desktop.
Twilio Generate Random Number Function

project page: https://michaellunzer.com/projects/home-assistant-wake-up-alarm-with-a-phone-call

Twilio Studio Flow Screenshot: https://images.ctfassets.net/o4gibwve10tr/31Li8fctiN6m1gZfoXyaFl/6a524701c1a2647899ebb20e2c03ee63/Screen_Shot_2021-03-09_at_9.25.00_PM.png

Call To Action:

fn_generate_random.js returns a number (x), an operator (+, -, multiply, /divide/), and another number (y).

Within the /divide/ operator, I check if it is a positive whole number (using regex ^[1-9][0-9]*$), make sure you don't divide by 1, and then make sure you aren't dividing by itself.

Within the main flow, there is a bit of a delay on the phone because it has to generate the number and test all these conditions until it finds one that randomly works. Do you think it'd be easier to add the math criteria into the javascript file? Or should I just play a little music for the 2-5 seconds it takes to calculate so the user doesn't worry that something is wrong.

exports.handler = function(context, event, callback) {
let x = Math.floor(Math.random() * (9 - 1) + 1);
let y = Math.floor(Math.random() * (9 - 1) + 1);
const operators = ['+', '*', '-', '/'];
function randomOperator(operators) {
return operators[Math.floor(Math.random() * operators.length)];
}
let o = randomOperator(operators);
let z = eval(x + o + y);
// console.log(x + " " + o + " " + y);
// console.log("z = " + z);
// console.log("o = " + o);
let response = {o:o, x:x, y:y, z:z}
callback(null, response);
};
@tchesnutt
Copy link

tchesnutt commented Apr 2, 2021

First off, happy birthday!

I'm going to be honest, I don't really get the Flow Screenshot. Adding some sort of feedback while the calculation is probably a good idea in general, especially if the rate of evaluation changes between runtime. Now, I'm not sure if I'm reading this right, but it looks like, worst case scenario, this could run forever if you're just really unlucky when generating numbers for finding divisors with no remainder.

I'm assuming that you want 'x' and 'y' to be between nine and one. If that is the case, then to make the evaluation time more predictable, you could solve your equation backwards.

  1. Pick an operator
    2a. For operators '' and '+', pick a random number between one and the greatest possible number (81 for '', 18 for '+')
    2b. For operators '-' and '/', pick a random number between nine and the lowest possible number (one or two for '/', depends on how you're feeling)
  2. Then just solve for all the clean solutions
  3. Pick one

If you want I could refactor fn_generate_random.js to do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment