Skip to content

Instantly share code, notes, and snippets.

@nofishleft
Created June 8, 2020 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nofishleft/481fb37f06573e8aa72bdd69111266e1 to your computer and use it in GitHub Desktop.
Save nofishleft/481fb37f06573e8aa72bdd69111266e1 to your computer and use it in GitHub Desktop.
Prints all combinations attainable with this lfsr implementation and seed value. It also graphs the distribution.
def incr (rand):
return [rand[1], rand[2], rand[3] ^ rand[0], rand[4], rand[5], rand[6], rand[7], rand[8] ^ rand[0], rand[0]]
# Check if the last combination equals the seed/starting combination
def equal(a, b):
for i in range(len(a)):
if a[i] != b[i]:
return False
return True
# Contains list of all combinations, starting with the seed
rand_list = [[1,0,1,0,1,0,0,1,1]]
i = 0
print(rand_list[0])
rand_list.append(incr(rand_list[i]))
while equal(rand_list[0],rand_list[i+1]) == False:
i += 1
rand_list.append(incr(rand_list[i]))
print(rand_list[i])
print(rand_list[-1]) # Duplicate
print(f"{2**9} Total Combinations Based On Bits")
print(f"{len(rand_list)-1} Unique Combinations With These Seed") # -1 to exclude the duplicate
def bin_to_int(bin):
value = 0
for i in range(len(bin)):
if bin[i] == 1:
value += pow(2,i)
return value
int_list = [bin_to_int(x) for x in rand_list]
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.distplot(int_list, bins=10, kde=True, rug=False)
plt.title('LFSR Distribution')
plt.ylabel('Probability Density')
plt.xlabel('Decimal Value')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment