我正在尝试在 R 中模拟中餐厅流程,并想知道是否可以对这种粗略的实现进行任何效率改进。
iTables = 200 # number of tables
iSampleSize = 1000 # number of diners
# initialize the list of tables
listTableOccupants = vector('list', iTables)
for(currentDiner in seq.int(iSampleSize)) {
# occupation probabilities for the next diner
vProbabilities = sapply(listTableOccupants,
function(x) ifelse(!is.null(x),
length(x)/currentDiner,
1/currentDiner))
# pick the index of the lucky table
iTable = sample.int(iTables, size = 1, prob = vProbabilities)
# add to the list element corresponding to the table
listTableOccupants[[iTable]] =
c(listTableOccupants[[iTable]], currentDiner)
}
特别是,我担心这条线:
# add to the list element corresponding to the table
listTableOccupants[[iTable]] =
c(listTableOccupants[[iTable]], currentDiner)
这有效率吗?