2

我正在尝试在 Captivate 中创建一个有趣的游戏。游戏将模仿命运之轮或旋转轮盘形式。

我正在尝试对其进行不同的旋转,并且没有任何重复的数字。

这是我到目前为止所拥有的:

 function getRandomInt (min, max) {
var jsRandomNumber = Math.floor (Math.random () * (max - min + 1)) + min;

    if(jsRandomNumber==1){
      window.cpAPIInterface.setVariableValue('cpCmndGotoSlide', 1);
    }else if (jsRandomNumber==2){
      window.cpAPIInterface.setVariableValue('cpCmndGotoSlide', 2);
    }else if (jsRandomNumber==3){
      window.cpAPIInterface.setVariableValue('cpCmndGotoSlide', 3);
    }else if (jsRandomNumber==4){
      window.cpAPIInterface.setVariableValue('cpCmndGotoSlide', 4);
    }else if (jsRandomNumber==5){
      window.cpAPIInterface.setVariableValue('cpCmndGotoSlide', 5);
    }else if (jsRandomNumber==6){
      window.cpAPIInterface.setVariableValue('cpCmndGotoSlide', 6);
    }else if (jsRandomNumber==7){
      window.cpAPIInterface.setVariableValue('cpCmndGotoSlide', 7);
    }else if (jsRandomNumber==8){
      window.cpAPIInterface.setVariableValue('cpCmndGotoSlide', 8);
    }


}

getRandomInt(1,8);

我不明白如何防止重复数字。如果已查看幻灯片,我也可以在 Captivate 中分配值,但是如果生成随机数,我不明白如何跳过该幻灯片。任何帮助是极大的赞赏。

4

1 回答 1

0

您可以让数组存储待处理的内容并继续删除已显示的内容。

function getRandomInt () {
  var min = 0;             //1st arr element index
  var max = arr.length-1;  // last arr element index

  if(max<0){
    return false;          //else repopulate the array or based on what you want
  }
  var randomIndex = Math.floor (Math.random () * (max - min + 1)) + min;
  var randomNumber = arr.splice(randomIndex,1)[0];
  //extract the item in that randomIndex position, 
  //since the array is global it will affect the array next time, 
  //we will not have that value in the next call.

  //window.cpAPIInterface.setVariableValue('cpCmndGotoSlide', randomNumber);
  console.log(randomNumber);
}

//generate a random array to keep track
var arr = function(){
  var min = 1, max = 8, atr = [];
  for(var i=min; i<=max; i++){
    atr.push(i);
  }
  return atr;
}();


//run a few times to test.
getRandomInt();
getRandomInt();
getRandomInt();
getRandomInt();
getRandomInt();

于 2015-09-22T19:04:49.800 回答