我有一个10个概率列表,我想要转换为线段,以便我可以执行轮盘赌选择。如何转换为线段?
概率清单:
[0.17 0.15 0.14 0.14 0.11 0.1 0.06 0.06 0.04 0.03]
转换成细分时应该是:
[0.17 0.32 0.46 0.60 0.71 0.81 0.87 0.93 0.97 1.0]
现在这是我的代码:
to calculate-s
let i 1 ;; previously added a 0 to list using set p-list fput 0 p-list
while [i < 11] [
ask turtles [
if [p] of self = item i p-list [
let s (p + item (i - 1) p-list)
]
]
set i (i + 1)
]
end
但当然它只是将当前概率与前一概率相加,所以得到:
[0.17 0.32 0.29 0.28 etc]
答案 0 :(得分:-1)
我不确定线段的确切含义,但是如果你只想要一个自包含的块来创建一个列表
newlist[i] = (oldlist[i] + (newlist[i - 1]))
您可以使用foreach
逐步浏览旧列表并生成新的汇总值列表,如下所示。
to make-segments
let oldlist [0.17 0.15 0.14 0.14 0.11 0.1 0.06 0.06 0.04 0.03]
let newlist []
let n 0
foreach oldlist [
[x]->
ifelse n < 1 [ ;;; if index is 0 you can't index 0 -1, so just use item 0
set newlist lput item 0 oldlist newlist
]
[ ;;; else, add the item n from the old list to the
;;; previous item in the new list.
set newlist lput (precision (item n oldlist + item (n - 1) newlist) 2) newlist
]
set n n + 1
]
print newlist
end