treatment_indexes
将被创建,所以您将随机分配它们在
treatment_selecion_id
水平。在你的结果中,你应该看到
治疗选择
数字相同的人会有不同的结果
treatment_index
. 但是你找不到
repetition_index
治疗指数
.
你可以创造这个
治疗指数
但是你仍然需要搜索
重复性指数
treatment_selection_id
和
具有相同的值,这将符合您所要求的唯一性行为。
这种变化应该给你随机性,仍然消除了重复的
重复性指数
no_reps = @trial.number_of_repetitions
repet_treat = {} # you have to keep track of repetition_indexes and treatment_indexes
@trial.treatment_selections.each do |r|
r.repetitions.in_groups_of(no_reps).each_with_index do |a, i|
treatment_indexes = (1..no_reps).to_a # randomness will be handled elsewhere
a.each_with_index do |r, j|
r.repetition_index = j + 1
# store treatment_index for this repetition_index in an array
repeat_treat[r.repetition_index] ||= []
# get the treatment_index you have already used for this repetition_index
used_treat_indexes = repeat_treat[r.repetition_index]
# delete the used indexes from the posibilities of the next and get a new random index
r.treatment_index = (treatment_indexes - used_treat_indexes).sample
# store your newely used treatment_index in its repetition_index group
repeat_treat[r.repetition_index] << r.treatment_index
end
end
end
结构
@trial
正在存储
治疗指数
@审判
而不是一个新的散列。
no_reps = @trial.number_of_repetitions
@trial.treatment_selections.each do |r|
r.repetitions.in_groups_of(no_reps).each_with_index do |a, i|
treatment_indexes = (1..no_reps).to_a # still handle randomness elsehwere
a.each_with_index do |r, j|
r.repetition_index = j + 1
used_indexes = @trial
.treatment_selections
.map(&:repetitions)
.select { |rep| rep.repetition_index == r.repetition_index }
.map(&:treatment_index)
r.treatment_index = (treatment_indexes - used_indexes).sample
end
end
end