#!/usr/bin/env python
 
from random import randint
 
A = [0,1,2]
 
 
def swap(a, src, dst):
	temp = a[dst]
	a[dst] = a[src]
	a[src] = temp
 
 
def false_randomize(array):
	a = array[:]
	for i in range(len(a)):
		dest = randint(0, len(a)-1)	# Notice here we take our swap target index with equal probability in the range 0..N
		swap(a, i, dest)
	return a
 
 
def true_randomize(array):
	a = array[:]
	for i in range(len(a)):
		dest = randint(i, len(a)-1)	# Here our destination can only come from i..N
		swap(a, i, dest)
	return a
 
 
 
perms = [[0,1,2], [0,2,1], [1,0,2], [1,2,0], [2,0,1], [2,1,0]]
 
bins_false = [0]*6
bins_true = [0]*6
 
max_iterations = 100000
for i in range(max_iterations):
	bins_false[perms.index(false_randomize(A))] += 1
	bins_true[perms.index(true_randomize(A))] += 1
	if i%1000 == 0:
		print "iteration:", i
 
 
print "False randomize, permutation distribution:", [val/(1.0*max_iterations) for val in bins_false]
print "True randomize, permutation distribution:", [val/(1.0*max_iterations) for val in bins_true]

Alumni Liaison

Ph.D. 2007, working on developing cool imaging technologies for digital cameras, camera phones, and video surveillance cameras.

Buyue Zhang