Generating binary sequences without repetition
Generate batch
number of int
in range(0, 2**dim + 1)
Convert these numbers to binary, then convert to sequence of 0
a and 1
s.
from random import sample
def generate(batch, dim):
my_sample = [f'{n:0>32b}' for n in sample(range(2**dim+1), batch)]
return [[int(n) for n in item] for item in my_sample]
def generate2(batch, dim):
return [list(map(int, f'{n:0>32b}')) for n in sample(range(2**dim+1), batch)]
the second one is bit faster
from timeit import timeit
print(timeit("generate(1000, 32)", setup="from __main__ import generate", number=100))
print(timeit("generate2(1000, 32)", setup="from __main__ import generate2", number=100))
output
1.4956848690007973
1.1187048860001596