目前自己写的版本
def sample_group(array, group_array = [2,3,4,5])
group = []
while array.size > 0
rand = group_array.sample
group << array.first(rand)
array = array.drop(rand)
end
group
end
测试数组:[1,2,3,4,5,6,7,8,9]
结果:
[[1, 2, 3, 4], [5, 6, 7, 8], [9]]
[[1, 2], [3, 4], [5, 6, 7, 8, 9]]
[[1, 2], [3, 4, 5], [6, 7, 8, 9]]