I have a list of N integers and I iteratively take two integers at random, which are not at the same position in the list and uniformly recombine their binary representation, e.g. in intList = [1,4,1,5] (N=4) the number on the second and third position is chosen, aka 4 & 1, and their binary represenations 4=100 & 1=001 is uniformly recombined. Uniformly recombined means that I chose either the 1 or the 0 of the first position, one of the two 0's of the second position and either the 0 or 1 of the third position. This could result in 000 or 101 or 100 or 001. The result is saved as integer in the list. In each iteration I do this recombination for all N integers. This happens in a function with a numba decorator. My code is:
@nb.njit()
def comb():
iterations = 100000
N = 1000
intList = list(range(N)) # this is just an example of possible integer lists
l = 10 # length of the largest binary representation.
intList_temp = [0]*N
for _ in range(iterations):
for x3 in range(N):
intList_temp[x3] = intList[x3]
for x3 in range(N):
randint1 = random.randint(0, N - 1)
randint2 = random.randint(0, N - 1)
while randint1 == randint2:
randint1 = random.randint(0, N - 1)
randint2 = random.randint(0, N - 1)
a = intList[randint1]
b = intList[randint2]
c = a ^ ((a ^ b) & random.randint(0, (1 << l) - 1))
intList_temp[x3] = c
for x3 in range(N):
intList[x3] = intList_temp[x3]
return intList
print(timeit(lambda: comb(), number=1))
>>>2.59s
My question is, can this be improved?
(1 << l)throwsl is undefined\$\endgroup\$lshould be the length of the binary representation of the largest integer. If you chooselto be 10 in my example code it should work. \$\endgroup\$lbut I think that is not important. \$\endgroup\$