从阵列获取随机元素,显示它和循环 - 但是从来没有其次是同一元素 [英] Get random element from array, display it, and loop - but never followed by the same element

查看:75
本文介绍了从阵列获取随机元素,显示它和循环 - 但是从来没有其次是同一元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个很简单的随机函数,通过数组中的元素循环,并将其显示在一个接一个的。

I wrote a simple enough randomize function that loops through the elements in an array and displays them one after the other.

见这里。

function changeSubTitle() {
    var whatAmI = ["Webdesigner", "Drummer", "Techie", "Linguistics student", "Photographer", "Geek", "Coder", "Belgian", "Batman", "Musician", "StackExchanger", "AI student"];
    setTimeout(function () {
        $(".page-header > h2").animate({
            "opacity": 0
        }, 700, function () {
            $(this).text(whatAmI[Math.floor(Math.random() * whatAmI.length)]);
            $(this).animate({
                "opacity": 1
            }, 700, changeSubTitle);
        });
    }, 1000);
}

但是,显然是很可能的是相同的元素被显示两次,之后立即另一个。这是因为我每次随机数组我调用该函数。我将如何prevent后对方要显示正确的两次元素?

However, obviously it is very well possible that the same element is displayed twice, one immediately after the other. This happens because I randomize the array each time I call the function. How would I prevent an element to be displayed two times right after each other?

我想最直接的方式做到这一点是让随机化功能的的循环,运行循环,每一个元素被调用时,从数组删除索引和填充数组当它是空的。很多对SO问题考虑这个问题,但没有具体的矿井。我不知道如何在循环显示每个元素做到这一点。

I suppose the most straightforward way to do this is to get the randomize function out the loop, run the loop and each time an element is called, remove the index from the array and refill the array when it's empty. A lot of questions on SO consider this problem, but not as specific as mine: I'm not sure how to do this in the loop to display each element.

推荐答案

要给出一个甚至所有元素的机会,除了所使用的previous之一执行以下操作:

To give an even chance of all elements except the previous one being used do the following:

var i, j, n = 10;
setInterval(function () {
  // Subtract 1 from n since we are actually selecting from a smaller set
  j = Math.floor(Math.random() * (n-1)); 
  // if the number we just generated is equal to or greater than the previous
  // number then add one to move up past it
  if (j >= i) j += 1;
  document.write(j + ' ');
  i = j;
}, 1000);

在code的意见应解释如何工作的。需要记住的重要一点是,你实际上是从9个可能的值中选择,而不是10。

The comments in the code should explain how this works. The key thing to remember is that you are actually selecting from 9 possible values, not from 10.

您应该初始化我是阵列中的随机元素开始之前。

You should initialize i to be a random element in the array before starting.

对于一个简单的散步经过3元素数组与选择的第二个元素:

For a simple walk through on a 3 element array with the second element selected:

I = 1,N = 3

的随机结果使我们0或1

The random result gives us either 0 or 1.

如果是0,那么 J> = I 返回false,我们选择元素零

If it is 0 then j >= i returns false and we select element zero

如果是1,则 J>。= I 返回true,我们选择第三个元素

If it is 1 then j >= i returns true and we select the third element.

您可以与我是0和我是2地看到,它永远不会溢出缓冲区,并始终有一个平等的机会选择所有其他元素通过做同样的路程。

You can do the same walk through with i being 0 and and i being 2 to see that it never overruns the buffer and always has an equal chance to select all other elements.

您可以那么同样的逻辑扩展到任何大小的数组。它的工作原理完全一样的方式。

You can then extend that same logic to an array of any size. It works exactly the same way.

这篇关于从阵列获取随机元素,显示它和循环 - 但是从来没有其次是同一元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆