How to Randomly Generate a Pattern Lock pt. 2

The last how-to I published had some backstories. It’s always easier to input a pattern lock when it only has 8-direction: up, down, left, right and 4 diagonals. But security-wise, having more options is always more preferable. This is one of the weakness of the pattern lock; you might as well use a simple PIN code for better security.

There is, however, one move that people may or may not want to have on the pattern lock, which is the chess knight move (L-move, or 1 forward and 1 diagonal). Compared to skipping a dot, I think it’s still in the realm of intuitively plausible. But for better or worse, anyone with dexterity issues, or simply don’t want any nonsense while putting in the pattern may not like it. That’s why I skipped it. But here’s the version that includes it.

Preface

Again, the Python script is essentially the same other than it allows pattern for 8-directions and the chess knight move. With it, the generator is more likely to hit all 9 dots before it halts with “incomplete pattern”. There’d be one benefit. The other being it simply adds more probability, albeit only small bit.

I probably won’t write a part. 3, because at that point we are basically just rearranging 1-9 randomly. Also, I’ve always thought it’s counterintuitive to jump the dots (e.g. connecting 1-3), even though it can make the pattern more complex.

Another note, as I understand it, most pattern locks only ask at least 4 dots to be used. You don’t have to use all 9 dots. You can totally use only 6 or 7 dots from the generator. The choice is entirely up to the user. Just be mindful of the fact the pattern lock isn’t the best method of authentication; always look for alternatives, if possible.

The instruction is identical as before. Only the contents of the script has changed.

Instructions

You can create and save a text file if you wish to run the script from it. You could also copy the contents into the Terminal while running Python; it will still generate the pattern the same.

#!/usr/bin/env python3

"""
1-2-3
4-5-6
7-8-9
"""

import random

print("1-2-3")
print("4-5-6")
print("7-8-9")
poss = [1,2,3,4,5,6,7,8,9]
numEx = {
	1: [3,7,9],
	2: [8],
	3: [1,7,9],
	4: [6],
	5: [],
	6: [4],
	7: [1,3,9],
	8: [2],
	9: [1,3,7]
}
n = random.randrange(1, 9+1)
pattern = [n]
poss.remove(n)
for idx in range(8):
	cand = [i for i in poss if not i in numEx[n]]
	if len(cand) == 0:
		print("Incomplete Pattern")
		break
	n = random.choice(cand)
	pattern.append(n)
	poss.remove(n)
	cand.clear()
print(pattern)

In case you are wondering what the numbers represent in patterns, they are the positions of dots. The script will print both the dots-representation (e.g. 1 for top-left corner) and the pattern it generated. If you are still unsure, think of number pads on phones.

Also don’t forget to either memorize or save the pattern in a secure location, such as password managers. It’s still an authentication, and you will need it again in the future.

Leave a comment