Random Pause with Simple Probability Distribution

I am following the random pause post but want to create a simple probability distribution. I wrote the following code but can’t get it to work in executeScript_Sandbox

//random pause between 5 and 10 seconds
//  var pause = 5000 + Math.floor(Math.random()*5000);
  
//  return pause;

function pauseGenerator() {
  //random pause between 10 and 20 seconds
  var pauseA = 10000 + Math.floor(Math.random()*10000);
  
  //random pause between 20 and 40 seconds
  var pauseB = 20000 + Math.floor(Math.random()*20000);
  
  //random pause between 60 and 90 seconds
  var pauseC = 60000 + Math.floor(Math.random()*30000);

  //percentage chance of each pause
  var factorA = .80;
  var factorB = .15;
  var factorC = .05;
    
  var randomNo = Math.floor(Math.random());
  var selectedPause;
  
  if (randomNo <= factorA) {
    selectedPause = pauseA;
  } else if (randomNo > factorA && randomNo <= (factorA + factorB)) {
    selectedPause = pauseB;
  } else {
    selectedPause = pauseC;
  }

  return selectedPause;
}

Here’s the original post:

I think you can not use a function call inside executeScript_Sandbox .But once you remove the “function… (){…}” part and use it as plain code, all works fine. Useful code snippet, btw :wink:

image

This macro works:

{
  "Name": "random",
  "CreationDate": "2020-10-19",
  "Commands": [
    {
      "Command": "executeScript_Sandbox",
      "Target": "  //random pause between 10 and 20 seconds\n  var pauseA = 10000 + Math.floor(Math.random()*10000);\n  \n  //random pause between 20 and 40 seconds\n  var pauseB = 20000 + Math.floor(Math.random()*20000);\n  \n  //random pause between 60 and 90 seconds\n  var pauseC = 60000 + Math.floor(Math.random()*30000);\n\n  //percentage chance of each pause\n  var factorA = .80;\n  var factorB = .15;\n  var factorC = .05;\n    \n  var randomNo = Math.floor(Math.random());\n  var selectedPause;\n  \n  if (randomNo <= factorA) {\n    selectedPause = pauseA;\n  } else if (randomNo > factorA && randomNo <= (factorA + factorB)) {\n    selectedPause = pauseB;\n  } else {\n    selectedPause = pauseC;\n  };\n\nreturn selectedPause;",
      "Value": "p"
    },
    {
      "Command": "echo",
      "Target": "${p}",
      "Value": "green"
    },
    {
      "Command": "pause",
      "Target": "${p}",
      "Value": ""
    }
  ]
}

Ok noted. It works great now, thanks