English 中文(简体)
a. 自动随机生成一个向N发送的未签字内壳体。
原标题:Uniformly randomly generate a vector of k unsigned ints that sums to N

另一种措辞是:任意分割 同样的物品进入了 k,使一些桶空。

讨论:

  • an "integer partition of N", to match the usual definition and counting, can be considered as:
    • a tuple of positive integers, in decreasing order, which sums to N
  • a vector of unsigned integers is a "partition" of N, if the sum of its elements (without integer overflow) is N.

我想写出一种功能 f(N,k),即随机和统一地选择了N分治可能长效的病媒,并回收选定的病媒。

如果能找到一个适用于所有k>、=1,但我特别关心k >、N。 因此,如果它有助于关注或限制这种条件,即证明。 而且,如果我们必须研究近似/湿度,那么把大部分病媒分录为零(至少是千兆字节;2N)就算得太高。


我的初步想法是:

  1. If N is small enough that it is reasonable to calculate (or look up in a table?) the number of integer partitions of N, then maybe we could proceed as:
    • Create a vector of k unsigned ints initialized to zero
    • Make a random integer partition of N. Let m be the length of this tuple.
    • Place those values in the initial m positions of the vector.
    • Randomly shuffle the vector.

这种做法将尽可能地对待生产媒介将含有N的条目与含有1的N条目一样。 这是正确的。 但是,可能有一种简单的权重,可以适用于“任意分割N”的情况,而这种权衡是正确的?

  1. Another approach which feels cleaner, but would likely still need "re-weighting" somewhere:
    • Create a vector of k unsigned ints initialized to zero
    • do the following N times:
      • randomly choose an element of the vector and increment it

虽然这感觉更清洁,但我认为,这将大大提高“再加权”的难度。 虽然第1部分的权重像对我来说是一个困难的算法问题,但我至少可以想象需要计算什么。 在这方面,我甚至不敢确定需要重新加权的内容和方式。

The reason I think it likely still needs reweighting is that there is exactly one sequence of random choices that would lead to the vector looking like [N,0,0,...,0], and N! sequences of random choices that would lead to the vector starting with N ones [1,1,...,1,0,0,...,0]. Calculating the ratios of these "incorrect weighting" of the final result sounds doable, but I don t know how I d go about reweighting the individual steps to correct for it.

  1. Or maybe there is another approach entirely, that I have not thought of?
最佳回答

Generate a random int in the range 0-n k-1 times. Treat these as partitions of [1, 1, ..., 1] <- size n. Then the sums between partitions (& endpoints) are your vector.

例如,N=2,k=5:

say we get 0, 1, 1, 2

我们认为:

我们将其解释为[0、1、0、1、0](缩小差距为零)。

如果我们拥有0,2,2,2,那么我们就拥有[<0>,1>,22,00]。

Here s Ruby code for this:

def f(n, k)
  arr = [0]
  ans = []
  (k-1).times do
    arr.append(rand(n+1))
  end
  arr.append(n)
  arr.sort!
  1.upto(k) do |i|
    ans.append(arr[i] - arr[i-1])
  end
  return ans
end

运行时间是O(klog k),因为情况不同。 我们可能能够避免这种局面,产生按顺序排列的随机数字。

——————————

这并不统一。 这里有100万只(4,2个)

1_000_000.times do
  m[f(2,4)] += 1
end
=> 1000000
> m
=> 
{[1, 0, 1, 0]=>205646,
 [0, 1, 1, 0]=>411624,
 [0, 0, 1, 1]=>205916,
 [0, 1, 0, 1]=>205144,
 [0, 0, 2, 0]=>205961,
 [0, 0, 0, 2]=>68718,
 [1, 0, 0, 1]=>68178,
 [0, 2, 0, 0]=>205736,
 [2, 0, 0, 0]=>68275,
 [1, 1, 0, 0]=>205783}

-——————————-

Stars和驳船工程。 这里是《鲁比法典》和《另100万》:

def g(n,k)
  arr = [1]*n + [0]*(k-1) # 1 s represent stars (what we re counting), and 0 s represent bars (separators)
  arr.shuffle!
  ans = []
  sum = 0
  arr.each do |val|
    if val == 0
      ans.append(sum)
      sum = 0
    else
      sum += val
    end
  end
  ans.append(sum)
end

1_000_000.times do
  m[g(2,4)] += 1
end
=> 1000000
> m
=> 
{[1, 1, 0, 0]=>99977,
 [0, 2, 0, 0]=>100150,
 [1, 0, 0, 1]=>100201,
 [0, 1, 1, 0]=>100034,
 [0, 1, 0, 1]=>99422,
 [0, 0, 0, 2]=>99865,
 [2, 0, 0, 0]=>99662,
 [1, 0, 1, 0]=>100359,
 [0, 0, 1, 1]=>100332,
 [0, 0, 2, 0]=>99998}
问题回答




相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签