随机播放列表(带有重复项)以避免相同的元素彼此相邻

时间:2021-01-26 13:42:44

I am wondering if there is a "best" way to shuffle a list of elements that contains duplicates such that the case where array[i] == array[i+1] is avoided as much as possible.

我想知道是否有一种“最好的”方法来重新组合包含重复项的元素列表,以便尽可能避免使用array [i] == array [i + 1]的情况。

I am working on a weighted advertising display (I can adjust the number of displays per rotation for any given advertiser) and would like to avoid the same advertister appearing twice in a row.

我正在进行加权广告显示(我可以调整任何给定广告客户的每次轮播的显示数量),并希望避免同一个广告连续出现两次。

5 个解决方案

#1


2  

This is pretty similar to this question. If you replace A, B, and C in the example given over there with your advertisers, I think you arrive at the same problem. Maybe some of the solutions suggested for that one can help you.

这与这个问题非常相似。如果您在广告客户的示例中替换A,B和C,我认为您遇到了同样的问题。也许为那个建议的一些解决方案可以帮助你。

#2


2  

Basic randomizing should cause enough dispersion in a large set.

基本随机化应该在大集合中引起足够的分散。

If you want to minimize that even more (which might not even be necessary depending on the sets), the simplest way would be to definitely find the close by dupes after randomization and move them around (but you might create patterns). A better approach might be to create subsets containing the side by side dupes and redo the randomization.

如果你想最大限度地减少它(根据集合甚至可能不需要),最简单的方法是在随机化之后绝对找到dupe并将它们移动(但你可能会创建模式)。更好的方法可能是创建包含并排欺骗的子集并重做随机化。

For a smaller set nothing might be possible, depending on the number of dupes. So the solution for a very small set would only be good basic randomization (And we're back at the first sentence).

对于较小的集合,根据欺骗的数量,可能无法进行任何操作。因此,一个非常小的集合的解决方案只是良好的基本随机化(我们回到第一句话)。

随机播放列表(带有重复项)以避免相同的元素彼此相邻

#3


1  

Personally I think the easiest way to hand this would be to randomize the array, and then iterate over it until you find 2 elements with the same value that are next to each other. When you find 2 of the same values beside eachother, move the later one to another spot in the array by iterating over the array until you find a spot such that it isn't beside another of the same value. If you can't find a value, just leave it where it is, and continue on with the next element of the array. This probably won't be the most optimal solution, but will be fine for smaller data sets, and probably the simplest to program.

我个人认为最简单的方法就是随机化数组,然后迭代它直到找到2个具有相同值的元素,彼此相邻。当您在彼此旁边找到2个相同的值时,通过迭代数组将后一个值移动到数组中的另一个点,直到找到一个点,使其不在另一个相同的值旁边。如果找不到值,只需将其保留在原来的位置,然后继续使用数组的下一个元素。这可能不是最优的解决方案,但对于较小的数据集可能会很好,并且可能是最简单的编程。

#4


0  

What's the biggest number of duplicates you may have? 2, 3, any?

您可能拥有的重复数量最多的是什么? 2,3,任何?

#5


0  

For reference, my (very) naive approach was something like this (actually using LINQ/SQL calls but this is simplified):

作为参考,我(非常)天真的方法是这样的(实际上使用LINQ / SQL调用,但这是简化的):

var advertisers = getAdvertisers();
var returnList = new List();
int totalWeight = sumOfAllAdvertisersWeight();
while (totalWeight > 0)
{
    for (int i=0; i<advertisers.Count; i++)
    {
        if (advertisers[i].Weight > 0)
        {
            returnList.add(advertisers[i]);
            advertisers[i].Weight--;
            totalWeight--;
        }
    }
}
return returnList;

This will avoid duplicates until the end but yeah it would pay to check backwards through the returnList afterwards and if there are any duplicates tailing, try and place them in the mix earlier.

这样可以避免重复,直到结束,但是之后通过returnList向后检查并且如果有任何重复拖尾,请尝试将它们放在混合中。

#1


2  

This is pretty similar to this question. If you replace A, B, and C in the example given over there with your advertisers, I think you arrive at the same problem. Maybe some of the solutions suggested for that one can help you.

这与这个问题非常相似。如果您在广告客户的示例中替换A,B和C,我认为您遇到了同样的问题。也许为那个建议的一些解决方案可以帮助你。

#2


2  

Basic randomizing should cause enough dispersion in a large set.

基本随机化应该在大集合中引起足够的分散。

If you want to minimize that even more (which might not even be necessary depending on the sets), the simplest way would be to definitely find the close by dupes after randomization and move them around (but you might create patterns). A better approach might be to create subsets containing the side by side dupes and redo the randomization.

如果你想最大限度地减少它(根据集合甚至可能不需要),最简单的方法是在随机化之后绝对找到dupe并将它们移动(但你可能会创建模式)。更好的方法可能是创建包含并排欺骗的子集并重做随机化。

For a smaller set nothing might be possible, depending on the number of dupes. So the solution for a very small set would only be good basic randomization (And we're back at the first sentence).

对于较小的集合,根据欺骗的数量,可能无法进行任何操作。因此,一个非常小的集合的解决方案只是良好的基本随机化(我们回到第一句话)。

随机播放列表(带有重复项)以避免相同的元素彼此相邻

#3


1  

Personally I think the easiest way to hand this would be to randomize the array, and then iterate over it until you find 2 elements with the same value that are next to each other. When you find 2 of the same values beside eachother, move the later one to another spot in the array by iterating over the array until you find a spot such that it isn't beside another of the same value. If you can't find a value, just leave it where it is, and continue on with the next element of the array. This probably won't be the most optimal solution, but will be fine for smaller data sets, and probably the simplest to program.

我个人认为最简单的方法就是随机化数组,然后迭代它直到找到2个具有相同值的元素,彼此相邻。当您在彼此旁边找到2个相同的值时,通过迭代数组将后一个值移动到数组中的另一个点,直到找到一个点,使其不在另一个相同的值旁边。如果找不到值,只需将其保留在原来的位置,然后继续使用数组的下一个元素。这可能不是最优的解决方案,但对于较小的数据集可能会很好,并且可能是最简单的编程。

#4


0  

What's the biggest number of duplicates you may have? 2, 3, any?

您可能拥有的重复数量最多的是什么? 2,3,任何?

#5


0  

For reference, my (very) naive approach was something like this (actually using LINQ/SQL calls but this is simplified):

作为参考,我(非常)天真的方法是这样的(实际上使用LINQ / SQL调用,但这是简化的):

var advertisers = getAdvertisers();
var returnList = new List();
int totalWeight = sumOfAllAdvertisersWeight();
while (totalWeight > 0)
{
    for (int i=0; i<advertisers.Count; i++)
    {
        if (advertisers[i].Weight > 0)
        {
            returnList.add(advertisers[i]);
            advertisers[i].Weight--;
            totalWeight--;
        }
    }
}
return returnList;

This will avoid duplicates until the end but yeah it would pay to check backwards through the returnList afterwards and if there are any duplicates tailing, try and place them in the mix earlier.

这样可以避免重复,直到结束,但是之后通过returnList向后检查并且如果有任何重复拖尾,请尝试将它们放在混合中。