I need help with some maths to randomise number patterns

Hi!

I’m currently working on a mobile puzzle/memory game based around of mobile device’s pattern lock screen!

Each time you ‘Unlock’ a phone, a lock screen will appear with a randomly generated sequence with no recurring numbers. This is done simply with an array of of numbers from 1-9, shuffled and resized to sequence limit.

ezgif.com-915a64b4e1.gif

Laid out in a grid, it appears like so:

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

I can’t create a system that will make a sequence where you don’t need to go ‘around’ a cell to reach another cell.

Example instead of: 3,7,9
I want 3,5,7,8,9 (although still only a pattern that is limited to the current sequence limit)

I’ve attempted a complicated system that randomly shuffles an array of 2D Vectors; 1 = 0,0. 2 = 1,0. 4 = 0,1. Then calculating each element of the 2d vector against the next in the sequence checking that the two had a difference larger than 2. If so it would remove or add one to x or y. The problem is that a new generated vector could then be a duplicate of a another element in the sequence. The problem is my current system is using a while loop and crashes after a 3rd element is added to the sequence.

Can anyone help?

Not sure if this is what you’re trying to do or how you’re getting stuff to work, but this is the solution I thought of off the top of my head.

1591bc2c720b6e34ccf848dd1a1512f1b3008525.jpeg

Int to Coord.JPG

Coord To Index.JPG

Edit: And it should be noted that my grid looks like
6-7-8
3-4-5
0-1-2

Edit: And it just occurs to me that the for loop could go from 1 - Max instead of 0 - (Max - 1)

Thanks! This worked perfectly!

Really appreciate the amount of effort you went to help me out, cheers man!