0

这是我在这里的第一篇文章。提前感谢您花时间阅读我的问题。

我是新手编码员。我在十年前获得了计算机科学的辅修课程。我有做一些简单编码的冲动,机会来了,所以我做到了!

在开发游戏时,我想运行一个程序来确定给定参数的给定结果的机会。我兴奋地达到了可以尝试的地步,但 Google Scripts 无法处理运行 60,000,000 个可能的场景来计算获胜百分比。

我得到了,“错误:超过了最大执行时间。”

我只是想找到我和运行这个程序之间的最短路径。想法:

1)有没有办法删除最大执行时间,让它花一整天?有没有其他方法可以让它在 Google Scripts 中运行?2)也许我可以通过输入随机数来运行较少数量的试验。有没有办法在 Google Scripts 中生成随机数?3) 除了 Google Scripts 之外,我还应该做这种事情吗?如果是这样,我应该研究一下适用于 Mac 的免费/负担得起的编译器吗?我尝试将它导入 Xcode,但我很困惑,似乎无法找到一个简单的地方进行编译。此外,将其导入“C”会产生一些兼容性问题;虽然我可能只需要把它吸起来并在这里重新组装它。

作为参考,这是将其超时的函数:

function dieFeeder(winCount, fSkill, fMagnitude, fHeart, fDie1, fDie2, fDie3, fDie4,   fDie5, cSkill, cMagnitude, cHeart, cDie1, cDie2, cDie3, cDie4, cDie5){
// a parent function to function questionMatrix, feeds the changing dice into it

 var matrixWinner;  

//This 'for' clause keeps going until all dice permutations have been tried out

 for (var i=0; i<60466176; i++){
//This part changes the dice to go through all combiations in a way similar to counting in base 6

if (cDie5 == 7){
  cDie5 = 1;
  cDie4 = cDie4+1;
}

if (cDie4 == 7){
  cDie4 = 1;
  cDie3 = cDie3 +1;
}  

if (cDie3 == 7){
  cDie3 = 1;
  cDie2 = cDie2 +1;
}  

if (cDie2 == 7){
  cDie2 = 1;
  cDie1 = cDie1 +1;
}  

if (cDie1 == 7){
  cDie1 = 1;
  fDie5 = fDie5 +1;
}  

if (fDie5 == 7){
  fDie5 = 1;
  fDie4 = fDie4 +1;
}  

if (fDie4 == 7){
  fDie4 = 1;
  fDie3 = fDie3 +1;
}  

if (fDie3 == 7){
  fDie3 = 1;
  fDie2 = fDie2 +1;
}      

if (fDie2 == 7){
  fDie2 = 1;
  fDie1 = fDie1 +1;
}  

cDie5 = cDie5 + 1;

//This part checks to see who wins and increases the winCount if it was the Favorite

matrixWinner = questionMatrix(fSkill, fMagnitude, fHeart, fDie1, fDie2, fDie3, fDie4, fDie5, cSkill, cMagnitude, cHeart, cDie1, cDie2, cDie3, cDie4, cDie5);

if (matrixWinner == 'favorite'){

  winCount = winCount +1;

}

}

  return winCount;

}
4

1 回答 1

0
  1. There is no way to lift the maximum execution time. The limit is there so that other users (like me) can have time to run our scripts too! The solution to this problem is to break up your problem into many subproblems.

    • One solution would be to continue executing your script while your running time is under some threshold (say, 3 minutes) (i.e. keep track of how long your script has been running). Then save all state relating to your script (variables, etc.). Save these to ScriptDb. Then have your script run on a 5-minute trigger. When your script runs again, it reads the values from ScriptDb and picks up where it left off.
  2. If you're looking for random numbers, use Math.random(). Google Apps Scripts is built off of javascript, so basic javascript functions are available.

  3. Relating to my answer to #2, what you have shown is entirely javascript, so you can just copy your code over to some webpage to run it. (For testing, you can use jsfiddle). Also you need to define if (cDie5 == 7){

于 2013-02-25T03:12:24.093 回答