列表中项目的随机分配,最大数量 [英] Random distribution of items in list up to a maximum count

查看:115
本文介绍了列表中项目的随机分配,最大数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个List项包含x个项目.

I have a List of items containing x items.

我想根据条件将这些物品随机分配到其他列表中:

I want to distribute these items randomly in other Lists with the conditions :

  • 每个列表的最大大小为y个(y = 4)
  • 每个物品最多只能使用z次(z = 5)
  • Each List has a maximum size of y item (with y = 4)
  • Each Item must be used a maximum of z times (with z = 5)

如果x不能同时被y和z整除,则列表包含少于y个项目也是可以的.

If x isn't divisible by both y and z, it's okay to have Lists containing less than y items.

我正在寻找这种方法的Java(从1.6到1.8)实现.谢谢!

I'm looking for a Java (from 1.6 to 1.8) implementation of such a method. Thanks!

编辑

这是我到目前为止尝试过的:

Here's what I tried so far :

List<Item> myItems;  // Initialized in an other part
int y, z;            // Initialized in an other part
int x = myItems.size();
List<List<Item>> myList = new ArrayList<List<Item>>();
int a = (int) Math.ceil((double)x/y);

Random random = new Random(new Random().nextInt());

for (int j = 0; j < a; j++) {
    myList.add(j, new ArrayList<Item>());
}

for (int i = 0; i < x; i++) {
    int r = random.nextInt(a);
    while (myList.get(r).size() >= y) {
        r = random.nextInt(a);
    }
    myList.get(r).add(myItems.get(i));
}

// Here myList is populated

推荐答案

以下代码应该可以工作.我在这里假设列表中的项目总数不应更改.

The following code should work. I've assumed here that the total number of items in the list shouldn't change.

public List<List<Item>> distribute(List<Item> list, int y, int z) {
    int x = list.size();
    int nLists = (int) Math.ceil((double)x/y);

    // Create result lists
    List<List<Item>> result = new ArrayList<>();
    for (int j = 0; j < nLists; j++)
        result.add(new ArrayList<Item>());
    List<List<Item>> outputLists = new ArrayList<>(result);

    // Create item count store
    Map<Item, Integer> itemCounts = new HashMap<>();
    for (Item item : list)
        itemCounts.put(item, 0);

    // Populate results
    Random random = new Random();
    for (int i = 0; i < x; i++) {
        // Add a random item (from the remaining eligible items)
        // to a random list (from the remaining eligible lists)
        Item item = list.get(random.nextInt(list.size()));
        List<Item> outputList = outputLists.get(random.nextInt(outputLists.size()));
        outputList.add(item);

        // Manage eligible output lists
        if (outputList.size() >= y)
            outputLists.remove(outputList);

        // Manage eligible items
        int itemCount = itemCounts.get(item).intValue() + 1;
        if (itemCount >= z)
            list.remove(item);
        else
            itemCounts.put(item, itemCount);
    }

    return result;
}

注意:上面的代码变异了原始列表.如果不希望这样,则应在开始时创建列表的副本.

使用以下输入:

list = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29]   
y = 4
z = 3

这给出了输出列表,例如:

This gives output lists such as:

[1,10,17,2]
[2,24,6,26]
[16,23,21]
[8,0,3,13]
[12,11,6,0]
[2,17,14,7]
[0,27,7,12]
[7,19,3]

在运行期间,项目027达到了z限制,并且不再使用.

During that run, items 0, 2, and 7 reached the z limit, and were not used again.

这篇关于列表中项目的随机分配,最大数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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