Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

列表均匀取样?

一个int类型的列表A,长度为a,从此列表中【均匀】取出长度为x的样本列表?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

以下代码均匀随机选取样本列表b[1:k]且保持原来在a[1:n]中的顺序。算法是:p[1:n]是一个1到n的随机排列,选择那些使p[i] <= ka[i]进入样本。

choose(a[1:n], k)
  b = {}
  p = {1, 2, ... n}
  for i from 1 to n
    // random(i, n) 返回i和n之间(包含)的随机整数
    swap(p, i, random(i, n))
    if p[i] <= k
      append a[i] to b
  return b

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...