<small id='SiLGB'></small><noframes id='SiLGB'>

      <i id='SiLGB'><tr id='SiLGB'><dt id='SiLGB'><q id='SiLGB'><span id='SiLGB'><b id='SiLGB'><form id='SiLGB'><ins id='SiLGB'></ins><ul id='SiLGB'></ul><sub id='SiLGB'></sub></form><legend id='SiLGB'></legend><bdo id='SiLGB'><pre id='SiLGB'><center id='SiLGB'></center></pre></bdo></b><th id='SiLGB'></th></span></q></dt></tr></i><div id='SiLGB'><tfoot id='SiLGB'></tfoot><dl id='SiLGB'><fieldset id='SiLGB'></fieldset></dl></div>

      <tfoot id='SiLGB'></tfoot>

    1. <legend id='SiLGB'><style id='SiLGB'><dir id='SiLGB'><q id='SiLGB'></q></dir></style></legend>
          <bdo id='SiLGB'></bdo><ul id='SiLGB'></ul>

        JavaScript 生成随机数而不重复

        时间:2023-06-13
        <tfoot id='NiXpT'></tfoot>
      1. <i id='NiXpT'><tr id='NiXpT'><dt id='NiXpT'><q id='NiXpT'><span id='NiXpT'><b id='NiXpT'><form id='NiXpT'><ins id='NiXpT'></ins><ul id='NiXpT'></ul><sub id='NiXpT'></sub></form><legend id='NiXpT'></legend><bdo id='NiXpT'><pre id='NiXpT'><center id='NiXpT'></center></pre></bdo></b><th id='NiXpT'></th></span></q></dt></tr></i><div id='NiXpT'><tfoot id='NiXpT'></tfoot><dl id='NiXpT'><fieldset id='NiXpT'></fieldset></dl></div>
            <bdo id='NiXpT'></bdo><ul id='NiXpT'></ul>

              <legend id='NiXpT'><style id='NiXpT'><dir id='NiXpT'><q id='NiXpT'></q></dir></style></legend>

              <small id='NiXpT'></small><noframes id='NiXpT'>

                <tbody id='NiXpT'></tbody>

                  本文介绍了JavaScript 生成随机数而不重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一些代码,其中包含 x 数量的项目.在这种情况下,视频,我想随机调用一个视频,但是如果当前已经调用的视频与随机数相同,我希望它生成另一个随机数,直到它是唯一的.

                  I have some code where I have an array of x amount of items. In this case, videos, and I want to randomly call a video, however if the current video already called is the same as the random number I want it to generate another random number until it's unique.

                  这是我的代码:

                  var videoLinks = [
                      ['<iframe id="vid" src="https://www.youtube.com/embed/nYm2G4MnSkY?autoplay=1" frameborder="0" allowfullscreen></iframe>'],
                      ['<iframe id="vid" src="https://www.youtube.com/embed/wAgZVLk6J4M?autoplay=1&start=5&end=45" frameborder="0" allowfullscreen></iframe>'],
                      ['<iframe id="vid" src="https://www.youtube.com/embed/ix9wpslKwBE?autoplay=1" frameborder="0" allowfullscreen></iframe>'],
                      ['<iframe id="vid" src="https://www.youtube.com/embed/OJJ-iLsQOPc?autoplay=1&iv_load_policy=3" frameborder="0" allowfullscreen></iframe>'],
                      ['<iframe id="vid" src="https://www.youtube.com/embed/rore790l_sk?autoplay=1&start=12&end=94" frameborder="0" allowfullscreen></iframe>'],
                  ];
                  
                  var randomNumber = function () {
                      var getRandomNumber = Math.floor(Math.random() * 5);
                      var random = videoLinks[getRandomNumber]
                      document.getElementById("videoWrapper").innerHTML = random[0];
                  };
                  
                  randomNumber(); // To call the function on load
                  

                  推荐答案

                  用一个变量来检查个数是否相同.

                  use a variable to check if the number is the same.

                  类似这样的:(使用 LastNumber 存储 lastNumber)如果它已经被使用,我们会再试一次)

                  something like this: (using LastNumber to store the lastNumber) if it allready is used we gonna try again)

                  var videoLinks = [
                      ....
                  ];
                  
                  var lastNumber = 0;
                  var randomNumber = function () {
                      var getRandomNumber = Math.floor(Math.random() * 5);  
                      if(getRandomNumber != lastNumber){
                          var random = videoLinks[getRandomNumber];
                          document.getElementById("videoWrapper").innerHTML = random[0];
                          lastNumber = getRandomNumber;
                      }else{
                          randomNumber();
                      }
                  };
                  
                  randomNumber(); // To call the function on load
                  

                  这篇关于JavaScript 生成随机数而不重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:是否有任何 JavaScript 标准 API 可以根据语言环境解析为数字? 下一篇:在不使用循环的情况下查找数字数组中的一项

                  相关文章

                    <i id='J1pfe'><tr id='J1pfe'><dt id='J1pfe'><q id='J1pfe'><span id='J1pfe'><b id='J1pfe'><form id='J1pfe'><ins id='J1pfe'></ins><ul id='J1pfe'></ul><sub id='J1pfe'></sub></form><legend id='J1pfe'></legend><bdo id='J1pfe'><pre id='J1pfe'><center id='J1pfe'></center></pre></bdo></b><th id='J1pfe'></th></span></q></dt></tr></i><div id='J1pfe'><tfoot id='J1pfe'></tfoot><dl id='J1pfe'><fieldset id='J1pfe'></fieldset></dl></div>
                    1. <legend id='J1pfe'><style id='J1pfe'><dir id='J1pfe'><q id='J1pfe'></q></dir></style></legend>
                    2. <small id='J1pfe'></small><noframes id='J1pfe'>

                      • <bdo id='J1pfe'></bdo><ul id='J1pfe'></ul>

                    3. <tfoot id='J1pfe'></tfoot>