The algorithm is just the last part:

class OneWayCollection:

”’This is a collection where you can find the length, but

you can only walk through it once; you do not have random

access to the values.”’

def __init__(self, listOfValues):

self.length = len(listOfValues)

self.iterator = iter(listOfValues)

def length(self):

return self.length

def getNext(self):

return self.iterator.__next__()

orig_list = OneWayCollection(

['alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta'])

def selectRandom(values, needed):

”’This returns ‘needed’ values selected uniformly at random from

‘values’, which can be a OneWayCollection.”’

result = []

available = values.length

assert needed < = available

while needed > 0:

item = values.getNext()

rand = random.random()

if rand < needed / available:

needed -= 1

result.append( item )

else:

available -= 1

return result

I had been getting confused between an algorithm for selecting k random items from a list of n items, and an algorithm for selecting a random item from a list with equal probability of selecting each item and WITHOUT having random access to the list.

First, an algorithm for selecting some number of random items from a list of items IN A RANDOM ORDER is as follows (in Python, since I tested it):

import math

import random

orig_list = ['alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta']

def selectRandom(values, needed):

”’This returns a new list of ‘needed’ random values from

the list ‘values’. It ALSO shuffles the list values.

This executes in time proportional to needed, assuming that

swapping 2 positions in the list happens in constant time.”’

def swap(i,j):

”’Swaps the values at locations i and j in values.”’

temp = values[i]

values[i] = values[j]

values[j] = temp

available_to_choose = len(values)

assert needed <= available_to_choose

already_picked = 0

while ( already_picked < needed ):

# pick a random number from [0..available-1]

random_choice = math.floor( random.random() * available_to_choose )

# swap the cell at ‘already_picked’ with the one ‘random_choice’ after it

swap(already_picked, already_picked + random_choice)

available_to_choose -= 1

already_picked += 1

return values[0:needed]

I hate to say this, but I’m not changing anytime soon to the person that sent the email. Just another example of non standard English.

]]>I refuse to do business with them now. ]]>

Raj Paul

Andy Johnson

Ajay Venkat

Kishore Kumar

Firaas Khan

Shawn Parker

Arshad Ali

Derrick Galande

Ron Hussy

Mak Wilson

Abhay Singh

Raj Thakur

Daniel Smith

Axel Wayne

Unfortunately, I cannot grant you the permission as I do not control copyright on that image. I obtained it from page 17 of “Chaos: Making a new Science” by James Gleick (ISBN 0140092501). There it appears to be credited to “Edward N. Lorenz / Adolph E Brotman”. I hope you will have luck following up on that reference.

By the way, I hope you are successful obtaining the permission — I think that particular image is extremely effective in conveying the important discovery and using the actual original computer printouts (rather than a simulation you create) is a nice historical connection.

]]>