- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我喜欢将分数拆分为 n 个位置的数组。
假设我的分数是 11,数组的大小是 12。然后我喜欢有一些数组,例如 11 个或 10 个和 2 个一半(0.5)。最后它应该总和为 11。
那么可能的分数是:
size = 12
possible_scores = (0..size).step(0.5).to_a
我可以创建一个包含 12 个位置的数组:
scores = Array.new(size) {0}
我可以从以下可能值中选择一个随机值:
[0, 0.5, 1].sample
我正在寻找一种检索随机数组的有效方法,如果可能的话,它不会有很多状态变量。我已经尝试在 while 循环中执行此操作:
while score < 0
并用随机值减少 score 的值,并跟踪设置的数组位置。但它变成了一段相当困惑的代码。
有什么解决办法吗?谢谢!
编辑:
对于这个例子,我想要一个总和为 11 的数组。所以任何一个
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.5, 0.5]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1]
或任何总和为 11 的组合。
最佳答案
参数和变量
给定:
tot
,所需总数,0.5
的整数或奇数倍;和size
, 总数0
的,0.5
的和1
是总数tot
,要求 size >= tot
.我们定义三个变量:
n0
等于零的数量;n0pt5_pairs
等于 0.5
的对数的;和n1
等于个数。案例 1:tot
是一个整数
我们要求:
0 <= n0pt5_pairs <= [tot, size-tot].min
请注意,因为 n1 = tot - n0pt5_pairs
, 2 * n0pt5_pairs + n1 = n0pt5_pairs + tot > size
如果n0pt5_pairs > size-tot
.即总数0.5
的和那些超过了size
如果 0.5
的数量对超过 size-tot
.
给定 n0pt5_pairs
的值满足上述要求,n0
和 n1
确定:
n1 = tot - n0pt5_pairs
n0 = size - 2*n0pt5_pairs - n1
= size - tot - n0pt5_pairs
因此我们可以随机选择一个随机三元组 [n0, 2*n0pt5_pairs, n1]
如下:
def random_combo(size, tot)
n0pt5_pairs = rand(1+[tot, size-tot].min)
[size-tot-n0pt5_pairs, 2*n0pt5_pairs, tot-n0pt5_pairs]
end
例如:
arr = random_combo(17, 11)
#=> [3, 6, 8]
这个用来生成数组
arr1 = [*[0]*arr[0], *[0.5]*arr[1], *[1]*arr[2]]
#=> [0, 0, 0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 1, 1, 1, 1, 1, 1, 1]
我们洗牌:
arr1.shuffle
#=> [1, 0, 0.5, 1, 0.5, 0, 1, 1, 0, 1, 1, 1, 0.5, 0.5, 1, 0.5, 0.5]
备注arr1.size #=> 17
和 arr.sum #=> 11
.
案例 2:tot
是0.5的倍数
如果
tot = n + 0.5
哪里n
是一个整数,0
的每一个组合的,0.5
的和1
的至少有一个 0.5
.因此,我们可以计算出 0
的数量。的和1
的,以及 0.5
的数量的超过一个。为此,我们只需减少 tot
通过 0.5
(使其等于一个整数)和size
一个,使用generate_for_integer
要解决该问题,则对于该方法返回的每个三元素数组,增加 0.5
的数量一个。
def generate(size, tot)
return nil if size.zero?
is_int = (tot == tot.floor)
tot = tot.floor
size -= 1 unless is_int
n0pt5_pairs = rand(1+[tot, size-tot].min)
[*[0]*(size-tot-n0pt5_pairs), *[0.5]*(2*n0pt5_pairs + (is_int ? 0 : 1)),
*[1]*(tot-n0pt5_pairs)].
shuffle
end
ge = generate(17, 10)
#=> [0, 1, 0, 1, 0.5, 0.5, 0, 0.5, 0.5, 1, 1, 1, 1, 0.5, 0.5, 0.5, 0.5]
ge.size #=> 17
ge.sum #=> 10.0
go = generate(17, 10.5)
#=> [0.5, 0.5, 0.5, 1, 0, 0.5, 0.5, 1, 1, 0.5, 1, 1, 0.5, 1, 0.5, 0.5, 0]
go.size #=> 17
go.sum #=> 10.5
关于Ruby Split number in ones or halves,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55936384/
我对 MySQL 比较陌生,我正在尝试编写一个函数来查找纬度列表的中位数。我在网上找到了解决这个问题的几个不同的解决方案,但我无法真正理解他们在做什么。我决定让一些东西更容易阅读和直观,但我似乎遇到了
我喜欢将分数拆分为 n 个位置的数组。 假设我的分数是 11,数组的大小是 12。然后我喜欢有一些数组,例如 11 个或 10 个和 2 个一半(0.5)。最后它应该总和为 11。 那么可能的分数是:
我正在尝试使用 gcc-4.9.1 编译 64 位 C 库。 在使用 -O2 编译时,我在不同的地方收到以下错误。 -O0 编译工作正常。 Error: error: extended registe
我有一个 xml 文件,其中包含许多像素单位,例如“12px”“20px”。我想将所有这些措施减半。也就是把“12px”换成“6px”等等。这可以用正则表达式完成吗? 奇数个像素可能会向上舍入(“7p
我正在研究各种特征对法院对特定犯罪的判决的影响。该数据集非常大(28928 个观测值,86 个 2 级单元)。我正在考虑是否使用 level1 和 level2 特征作为控制(level1 为大写字母
在尝试将用户定义的链接函数与随机效果 glmer 结合使用时,我遇到了一个我不知道如何解决的错误: Error: (maxstephalfit) PIRLS step-halvings failed
我正在尝试读取一种基于 xml 的文件格式,在 JAVA 中使用 SAX 称为 mzXML。它携带部分编码的质谱数据(具有强度的信号)。 这是感兴趣的条目的样子(关于它有更多信息): eJwB
我已经使用 Primeng 控件将我的 Angular 11 项目更新为 Angular 12。完成后,primeng pKeyFilter 在任何使用它的地方都会抛出错误。从头开始创建一个新的 An
我正在尝试使用gnls函数将逻辑增长曲线拟合到一些数据。 数据: structure(list(Nest = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9
我正在尝试使用 Gamma 错误系列在 R 中执行 glmer。我收到错误消息:“错误:(maxstephalfit) PIRLS 步减半未能减少 pwrssUpdate 中的偏差” 我的响应变量是花
从 Angular 11 迁移到 Angular 12 并弹出此问题: “错误:双向绑定(bind)“值”的属性和事件部分未绑定(bind)到同一目标。” 父页面组件: import { Compon
我是一名优秀的程序员,十分优秀!