具有不同概率的Netlogo随机抽样

时间:2018-06-15 15:43:10

标签: random netlogo

NetLogo中是否有办法将数组中每个项目的概率值关联起来并随机选择n项?像下面这样的功能。

let names ["pooh", "rabbit", "piglet", "Christopher"]
let probs [0.5, 0.1, 0.1, 0.3]
n-random-select 5 names probs

>>["pooh", "pooh", "pooh", "Christopher", "piglet"]

在Python中numpy.random.choice正是这样做的。

1 个答案:

答案 0 :(得分:5)

这正是内置let DocumentDirURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) let fileURL = DocumentDirURL.appendingPathComponent("x").appendingPathExtension("json") do { var readString = try String(contentsOf: fileURL) let x = readString.components(separatedBy: "\n") print (" midas \(x) ") for i in 1..<x.count{ do{ let y = x[i] print (" midas \(y) ") let b = convertToDictionary(text: y) print (" midas1 \(b) ") let date = try b?["date"] dictarr["date"] = "\(date)" print (" midas \(dictarr["date"]) ") 扩展程序的用途:https://ccl.northwestern.edu/netlogo/docs/rnd.html

rnd:weighted-n-of-list-with-repeats是您想要的原语。 dictdata.addObject(dictarr) but it gives me error override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) let datainrow = dictdata[indexPath.row] // cell.textLabel?.text = "\(datainrow.objectForKey("message")!)" // Configure the cell... return cell } 有点灵活,但也有点麻烦。您可以这样写rnd

numpy.random.choice

并像这样使用它(请记住,netlogo列表中没有逗号):

n-random-select