【问题标题】:Divide the number into random number of random elements?将数字除以随机元素的随机数?
【发布时间】:2012-05-05 12:36:37
【问题描述】:

如果我需要将例如 7 分成随机数量的随机大小的元素,我该怎么做?

所以有时我会得到 [3,4],有时会得到 [2,3,1],有时会得到 [2,2,1,1,0,1]?

我想这很简单,但我似乎无法得到结果。这是我试图在代码方面做的事情(不起作用):

def split_big_num(num):
    partition = randint(1,int(4))
    piece = randint(1,int(num))
    result = []
    for i in range(partition):
        element = num-piece
        result.append(element)
        piece = randint(0,element)
#What's next?
        if num - piece == 0:
            return result
    return result

编辑:每个结果数字都应小于初始数字,并且零的数量应不小于分区数。

【问题讨论】:

  • 指定随机数元素的含义。你的意思是每个子集的长度都有相同的被选中的概率吗?或者你的意思是每个子集都有相同的被选中的概率?这些意味着非常不同的东西。
  • 应该返回 [7] 吗?那么 [0,0,0,0,0,7] 呢?它们可能吗?
  • 对不起,我必须澄清一下,不,不 7s..
  • 对你可以有多少个零有任何限制吗?
  • 哇,好问题!是的,不超过分区数..

标签: python math random integer-division


【解决方案1】:

您必须定义“随机”的含义。如果你想要一个任意整数分区,你可以生成所有整数分区,并使用random.choice。请参阅python: Generating integer partitions 这将不会给出 0 的结果。如果您允许 0,您将不得不允许可能有无限个 0 的结果。

或者,如果您只想删除随机块,请执行以下操作:

def arbitraryPartitionLessThan(n):
    """Returns an arbitrary non-random partition where no number is >=n"""
    while n>0:
        x = random.randrange(1,n) if n!=1 else 1
        yield x
        n -= x

由于问题限制,每个数字都应该小于原始数字,这有点尴尬;如果您允许原始数字,它会更优雅。如果您想要 0,您可以执行 randrange(n),但除非有隐藏的原因您不共享,否则它没有意义。

edit 回应问题编辑:既然你希望“零的数量应该不小于分区的数量”,你可以在末尾任意添加 0:

def potentiallyInfiniteCopies(x):
    while random.random()<0.5:
        yield x

x = list(arbitraryPartitionLessThan(n))
x += [0]*len(x) + list(potentiallyInfiniteCopies(0))

这个问题很随意,我强烈建议你选择这个作为你的答案:

def arbitraryPartition(n):
    """Returns an arbitrary non-random partition"""
    while n>0:
        x = random.randrange(1,n+1)
        yield x
        n -= x

【讨论】:

  • 作品的数量呈指数级增长。因此,即使是一个适度的 n 值也会耗尽你所有的内存。
  • 感谢您如此周到的回复
【解决方案2】:

我会去下一个:

>>> def decomposition(i):
        while i > 0:
            n = random.randint(1, i)
            yield n
            i -= n

>>> list(decomposition(7))
[2, 4, 1]
>>> list(decomposition(7))
[2, 1, 3, 1]
>>> list(decomposition(7))
[3, 1, 3]
>>> list(decomposition(7))
[6, 1]
>>> list(decomposition(7))
[5, 1, 1]

但是,我不确定这种随机分布是否完全均匀。

【讨论】:

  • 太棒了!非常感谢!只要允许,我就会接受这个作为答案。
  • 不错的答案!次要观点:使用 n = rn.randint(0, i) 将允许 Stpn 想要的零。
  • @Akavall: 并且还允许它开始积累很多 0。
  • 好的,我看错了那部分,但他的例子中确实有一个零,但处理这个可能只是细节。
  • 这种分布远非完全均匀。您往往有太多的大数字,而且它们往往出现在开头。
【解决方案3】:

递归救援:

import random

def splitnum(num, lst=[]):
    if num == 0:
        return lst
    n = random.randint(0, num)
    return splitnum(num - n, lst + [n])

for i in range(10):
    print splitnum(7)

结果:

[1, 6]
[6, 0, 0, 1]
[5, 1, 1]
[6, 0, 1]
[2, 0, 3, 1, 1]
[7]
[2, 1, 0, 4]
[7]
[3, 4]
[2, 0, 4, 1]

【讨论】:

  • @AshwiniChaudhary:计算返回该序列的几率,这就是你的答案。
【解决方案4】:

此解决方案不插入 0(我不明白您对零规则的描述应该是什么),并且同样可能生成除原始数字之外的所有可能组合。

def split (n):
    answer = [1]
    for i in range(n - 1):
        if random.random() < 0.5:
            answer[-1] += 1
        else:
            answer.append(1)

    if answer == [n]:
        return split(n)
    else:
        return answer

【讨论】:

    猜你喜欢
    • 2013-11-14
    • 1970-01-01
    • 2021-12-22
    • 2011-04-18
    • 2011-06-14
    • 2010-12-18
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多