要写出一个真正随机(我意思是均匀分布)的洗牌程序并不容易,即使强如Jeff Atwood也是 会写错的。Jeff写错的版本是这样的:
//error!
for(int i=0;i<cards.Length;i++){
int n = rand.Next(cards.Length);
Swap(ref cards[i],ref cards[n]);
}
问题在于我们并不能轻易地看出这段程序生成的排列不是均匀分布的(容我想想,我现在看 得出么…)。
实际上,是有个O(n)的洗牌算法 Fisher-Yates,又称为Knuth-shuffle(Knuth在TAOCP介绍过 而得名):
//To shuffle an array a of n elements (indices 0..n-1):
for i from n − 1 downto 1 do
j <- random integer with 0 ≤ j ≤ i
exchange a[j] and a[i]
这个算法就地洗牌,但它需要预先知道洗牌数组的长度。它的正确性(也就是生成的排列是 否均匀分布)不难看出。
Knuth-shuffle还有另一种实现:
//To initialize an array a of n elements to a randomly shuffled copy of
//source, both 0-based:
a[0] <- source[0]
for i from 1 to n − 1 do
j <- random integer with 0 ≤ j ≤ i
a[i] <- a[j]
a[j] <- source[i]
这个实现的正确性可以这么考虑:对于n-1次random调用生成的n!次随机序列,总会对应不 同的排列,所以n!次排列是均匀分布的。
这个实现有一个坏处,它需要额外的数组来存储生成的排列。但它也有一个大大的好处,它 可以不用预先知道数组的长度:
//To initialize an empty array a to a randomly shuffled copy of source whose
//length is not know:
while source.moreDataAvailable
j <- random integer with 0 ≤ j ≤ a.length
if j = a.length
a.append(source.next)
else
a.append(a[j])
a[j] <- source.next
Knuth-shuffle做些修改还可以有别的应用。比如下面这个问题:
- 如何从未知长度的序列中均匀地随机选择一个数?要求最多只遍历一遍。
- 如何从未知长度的序列中均匀地随机选择k个数?要求最多只遍历一遍。
问题(1)参考程序:
import random
def random_element(seq):
"""Return an element chosen at random from `seq`."""
it = None
for n, elem in enumerate(seq):
if random.randint(0, n) == 0:
it = elem
return it
问题(2)参考程序:
import random
def random_sample(n, items):
results = []
for i, v in enumerate(items):
r = random.randint(0, i)
if r < n:
if i < n:
results.insert(r, v) # add first n items in random order
else:
#at a decreasing rate,replace random items
results[r] = v
if len(results) < n:
raise ValueError("Sample larger than population.")
return results
参考资料
- http://www.codinghorror.com/blog/2007/12/the-danger-of-naivete.html
- http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
- http://nedbatchelder.com/blog/201208/selecting_randomly_from_an_unknown_sequence.html
- http://stackoverflow.com/questions/12128948/python-random-lines-from-subfolders/12134726#12134726
没有评论:
发表评论