-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomSample.py
151 lines (115 loc) · 4.08 KB
/
randomSample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import random
import time
import math
def XORShiftRNG(seed):
r=seed
r ^= (r<<21)
r ^= (r>>35)
r ^= (r<<4)
return r
def randInt(min_val, max_val, seed):
#naive rand int. needs replacing
if (max_val-min_val)==0:
return max_val
return (XORShiftRNG(seed) % (max_val-min_val)) + min_val
def binaryTreeGetParent(index):
if index == 0:
return None
else:
return math.floor((index-1)/2)
def binaryTreeGetLeftSibling(index):
if index == 2**math.floor(math.log(index+1,2))-1:
return None
else:
return index-1
def binaryTreeGetRightSibling(index):
if index+1 == 2**math.floor(math.log(index+2,2)-1):
return None
else:
return index+1
def randomBucketSizeArray(size_min, size_max, num_buckets, seed):
#todo: generate a log_2 access only version of this
array=[]
array.append(randInt(size_min+1,size_max-1, seed))
i=1
while i < num_buckets:
parent = int(binaryTreeGetParent(i))
while (parent >= len(array)):
parent = int(binaryTreeGetParent(parent))
if i%2==1:
min_divider = binaryTreeGetLeftSibling(parent)
if min_divider == None:
min_divider = size_min+1
else:
min_divider = array[min_divider]+1
max_divider = array[parent]-1
else:
max_divider = binaryTreeGetRightSibling(parent)
if max_divider == None:
max_divider = size_max-1
else:
try:
max_divider = array[max_divider]-1
except(IndexError):
num_buckets = num_buckets + 1
i = i + 1
continue
min_divider = array[parent]+1
if (max_divider - min_divider <=1) or (min_divider==array[-1]):
num_buckets=num_buckets+1
i=i+1
continue
array.append(randInt(min_divider+1, max_divider-1, seed+i))
i=i+1
array.append(size_min)
array.append(size_max)
array.sort()
return array
def randomOfArray(array, min_loc, max_loc, seed):
return array[randInt(min_loc, max_loc, seed)]
'''def randomSample(array, size, seed):
#not true random sample. Divides array into buckets and selects from each.
# should be good enough, and should be much faster.
if size>len(array):
raise IndexError("Desired output array size is larger than input size")
#todo: check type and other things
subset= []
buckets = randomBucketSizeArray(0, len(array)-1, size, seed)
print(buckets)
for i in xrange(size):
index = rand_int(buckets[i], buckets[i+1], seed+i)
subset.append(array[index])
#not needed for HTM
#random.shuffle(subset)
return subset'''
#def randomSampleAt(index, array, size, seed):
# bucket_size=len(array)/size
# i = rand_int(index*bucket_size, (i+1)*bucket_size-1, seed)
# return array[i]
def randomSample(input_array, size, seed):
#Algorithm R implementation
#todo: the random tree division should be O(desired size) while this is O(input array)
#todo: random tree method also allows for O(log(n)) determination of the n'th item in a chosen sample
# given a seed (in case I just want to store the random number and not the entire list of connections)
sample = []
for i in range(size):
sample.append(input_array[i])
for i in range(size, len(input_array)):
r = randInt(0,i-1, seed + i)
if r < size:
sample[r] = input_array[i]
return sample
def naiveRandomSample(array, size, seed):
# O(size), but contains duplicates
if size>len(array):
raise IndexError("Desired output array size is larger than input size")
subset = []
for i in xrange(size):
index = randInt(0, len(array)-1, seed+i)
subset.append(array[index])
return subset
if __name__ == "__main__":
arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
random.seed(time.time())
sub = randomSample(xrange(10000),1000, 574386534)
print(sub)