Skip to content

Instantly share code, notes, and snippets.

@maciektr
Last active January 11, 2020 10:44
Show Gist options
  • Save maciektr/4449aedf5c447603d475402cbd56e693 to your computer and use it in GitHub Desktop.
Save maciektr/4449aedf5c447603d475402cbd56e693 to your computer and use it in GitHub Desktop.
Generating random, valid, polish tax payer identification number with simple Python snippet
def random_nip(self):
res = ''
sum = 0
weights = [6, 5, 7, 2, 3, 4, 5, 6, 7]
for i in range(8):
k = self.rand.randint(1 if i < 3 else 0, 9)
sum += weights[i] * k
res += str(k)
k = self.rand.randint(0, 9)
if (sum + (k * weights[8])) % 11 == 10:
k += (1 if k + 1 < 10 else -1)
res += str(k)
sum += k * weights[8]
res += str(sum % 11)
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment