如何在ruby中随机将数字拆分成几个数字?

时间:2014-09-26 00:46:58

标签: ruby arrays

如何将数字随机分成几个数字?

例如:我有一个数字30,我想随机将它分成几个数字,每个数字的大小在3-10之间,每个数字的大小彼此不同

结果可能像:[5,7,9,6,3],[9,10,3,8],......等

我试过,但我无法解决,请给我帮助。

5 个答案:

答案 0 :(得分:1)

非常好的拼图。我会选择:

class Fixnum
  def random_split(set = nil, repeats = false)
    set ||= 1..self
    set = [*set]
    return if set.empty? || set.min > self || set.inject(0, :+) < self
    tried_numbers = []
    while (not_tried = (set - tried_numbers).select {|n| n <= self }).any?
      tried_numbers << number = not_tried.sample
      return [number] if number == self
      new_set = set.dup
      new_set.delete_at(new_set.index(number)) unless repeats
      randomized_rest = (self-number).random_split(new_set, repeats)
      return [number] + randomized_rest if randomized_rest
    end   
  end
end

30.random_split(3..10)

一般来说,上面的代码涵盖了很多案例。您可以在没有任何参数的情况下执行它,然后它将假设它从1到指定数字中选择数字,并且结果集不应包含任何重复。您可以选择传递给定数量的集合。如果你通过[1,2,3,4,4,4],那么4次重复不会超过3次。如果第二个参数设置为true,则允许设置元素在结果中出现两次或更多次。

答案 1 :(得分:1)

分割号码称为integer partition。这是基于Marc-André Lafortune's recursive algorithm的解决方案:

def expand(n, max = n)
  return [[]] if n == 0
  [max, n].min.downto(1).flat_map do |i|
    expand(n-i, i).map{|rest| [i, *rest]}
  end
end

expand(30).select { |a| a.size >= 3 && a.size <= 10 }.sample(5)
#=> [[15, 3, 3, 3, 2, 2, 1, 1],
#    [9, 5, 4, 3, 2, 2, 2, 1, 1, 1],
#    [13, 10, 4, 2, 1],
#    [8, 8, 7, 2, 2, 1, 1, 1],
#    [8, 6, 4, 3, 3, 2, 1, 1, 1, 1]]

请注意,可能的分区数量非常大:30个包含5,604个分区,100个包含190,569,292个分区,1,000个包含2.4 × 1031个分区。

答案 2 :(得分:0)

不是世界上最好的算法(它做了一些试验和错误)但是嘿,现在CPU周期很便宜......这应该适用于每个数字:

def split_this_number_into_several_numbers_randomly(a_number, min_number_to_start_from)
  random_numbers = [0]

  until (random_numbers.inject(&:+) == a_number)
    random_numbers << rand(min_number_to_start_from..a_number/3) # replace 30 here, I assumed you wanted up to 1/3 of the original number
    if (r = random_numbers.detect { |x| random_numbers.count(x) > 1}) then random_numbers.delete(r) end # so we have all unique numbers
    random_numbers.pop if random_numbers.inject(&:+) >= a_number - min_number_to_start_from && random_numbers.inject(&:+) != a_number
  end
  random_numbers.delete_if{ |x| x == 0 }
end

当然还有一些代码来测试它:

all_true = true
1000.times do
  arr = split_this_number_into_several_numbers_randomly(30, 3)
  all_true == false unless arr.inject(&:+) == 30
  all_true == false unless arr.size == arr.uniq.size
end

p all_true #=> true

答案 3 :(得分:0)

OP在评论中已经确认随机选择要反映以下程序:&#34;选择3到10之间不同数字的所有组合([3],[3,4],[3] ,5,6,8,9],.. [9,10],[10]),抛弃所有不加总和为30的组合,并随机选择其中一个&#34;。以下是实现该方法的简单方法。可以提高效率,但这将是很多工作。

<强>代码

def arrays(sum, range)
  largest_sum = (range.first+range.last)*(range.last-range.first+1)/2
  (raise ArgumentError,
    "largest sum for range = #{largest_sum}") if sum > largest_sum 
  avail = [*range]
  b = -(2*range.last + 1.0)
  c = 8.0*sum
  min_nbr = ((-b - (b*b - c)**0.5)/2).ceil.to_i
  max_nbr = ((-1.0 + (1.0 + c)**0.5)/2).to_i
  (min_nbr..max_nbr).each_with_object([]) { |n, a|
    a.concat(avail.combination(n).select { |c| c.inject(:+) == sum }) }
end

注意min_nbrmax_nbr使用二次公式来确定可能总和为sum的不同数字的范围。

<强>实施例

sum = 30
range = (3..10)
arr = arrays(sum, range) # all combinations that sum to 30
  #=> [[3, 8, 9, 10], [4, 7, 9, 10], [5, 6, 9, 10], [5, 7, 8, 10],
  #    [6, 7, 8, 9],
  #    [3, 4, 5, 8, 10], [3, 4, 6, 7, 10], [3, 4, 6, 8, 9],
  #    [3, 5, 6, 7, 9], [4, 5, 6, 7, 8]]

(Solution time: well under 1 sec.)

10.times { p arr[rand(arr.size)] } # 10 random selections
  #=> [3, 4, 6,  8,  9]
  #   [3, 4, 6,  8,  9]
  #   [5, 7, 8, 10]
  #   [4, 5, 6,  7,  8]
  #   [3, 4, 5,  8, 10]
  #   [6, 7, 8,  9]
  #   [3, 4, 5,  8, 10]
  #   [4, 5, 6,  7,  8]
  #   [6, 7, 8,  9]
  #   [3, 4, 6,  7, 10]

sum = 60
range = (3..10)
arr = arrays(sum, range)
  #=> in `arrays': largest sum for range = 52 (ArgumentError)

另外两个......

sum = 60
range = (3..20)
arr = arrays(sum, range) # all combinations that sum to 60
arr.size
  #=> 1092
(Solution time: about 1 sec.)  
10.times { p arr[rand(arr_size)] } # 10 random selections
  #=> [12, 14, 15, 19]
  #    [3,  4,  6,  7, 11, 13, 16]
  #    [3,  6,  7,  9, 15, 20]
  #    [3,  8, 14, 17, 18]
  #    [3,  4,  5,  7, 10, 13, 18]
  #    [3,  5,  6,  7, 11, 13, 15]
  #    [5,  6,  7,  8, 14, 20]
  #    [4,  5,  9, 11, 15, 16]
  #    [4,  5,  8, 13, 14, 16]
  #    [3,  4,  5, 12, 16, 20]

sum = 100
range = (3..30)
arr = arrays(sum, range) # all combinations that sum to 100
arr.size
  #=> 54380
(Solution time: 3 or 4 minutes)
10.times { p arr[rand(arr_size)] } # 10 random selections
  #=> [3,  4,  6,  9, 11, 12, 15, 17, 23]
  #   [4,  5,  6,  7,  9, 13, 14, 17, 25]
  #   [4,  5,  6,  7, 11, 17, 21, 29]
  #   [9, 10, 12, 13, 17, 19, 20]
  #   [6,  9, 10, 23, 25, 27]
  #   [3,  4,  5,  6,  7,  8,  9, 14, 15, 29]
  #   [3,  4,  5,  6,  7,  8,  9, 15, 17, 26]
  #   [3,  4,  5,  6,  7,  8, 17, 22, 28]
  #   [3,  5,  6,  7,  9, 12, 13, 15, 30]
  #   [6,  8,  9, 10, 13, 15, 18, 21]

答案 4 :(得分:0)

我使用递归的版本

def split_number(n, acc = [])
  max = n.to_i - 1
  return n, acc if max.zero?

  r = rand(max) + 1
  remainder = n - acc.inject(0, :+) - r
  acc << split_number(remainder, acc).first if remainder > 0
  [r, acc].flatten
end

split_number(100)
# => [34, 1, 13, 1, 9, 4, 12, 21, 5]