Random changes in a list of binary numbers I have a list of N integers, e.g. intList=[1,7,3,1,5] with N=5 and all integers have a binary representation of lenght l, e.g. binList=[&quot;001&quot;,&quot;111&quot;,&quot;011&quot;,&quot;001&quot;,&quot;101&quot;] with l=3. Now I want to add random flips from 1-&gt;0 &amp; 0-&gt;1. Each of the l positions should flip with a probabiliy mu. (mu varies over many magnitudes &amp; N is around 100-100000) This happens over many iterations. Currently my code is
@nb.njit()
for x in range(iterations):
    ...other code...
    for number in range(N):
        for position in range(l):
            if random.random() &lt; mu:
                intList[number] = intList[number] ^ (1 &lt;&lt; position)
    ...other code...

The numba decorator is necessary. Since this part takes most of the execution time I just want to make sure that it is already optimal. Thanks for any comment!
You could replace
        for position in range(l):
            if random.random() &lt; mu:
                intList[number] = intList[number] ^ (1 &lt;&lt; position)

with
        for bit in bits:
            if random() &lt; mu:
                intList[number] ^= bit

after preparing bits = [1 &lt;&lt; position for position in range(l)] before the show and importing the random function.
Don't know whether that helps when using numba, and you didn't provide benchmark code.