0

I have a grid of pixels 64x8. The aim is to to activate the pixels on this grid in a random manner till the whole grid is activated.

Logically I can generate random numbers in 0-63 and 0-7 range and then activate this pixel. Assuming I run this for long enough, the grid should be completely activated.

However, I am wondering if there is any algorithm that can minimize / avoid altogether collision (returning already activated pixel coordinate) and guarantee complete grid activation in a finite amount of time?

Ankit
  • 431
  • 2
  • 5
  • 18
  • 5
    Just store all coordinates in an array and do a random_shuffle – Photon Apr 16 '20 at 10:00
  • Is randomizing a pixel the same thing as activating it? Also, why don't you iterate your grid of pixels sequentially and "activate" the pixels one-by-one? – Lajos Arpad Apr 16 '20 at 10:16
  • 1
    If you need a shuffle algorithm look at https://stackoverflow.com/questions/6127503/shuffle-array-in-c – Jerry Jeremiah Apr 16 '20 at 10:16
  • Refering to the first comment: No need to store the coordinates, just fill an array with numbers from 0 to 511 (64x8 = 512) , (array will contain {0,1,2,3,..., 511}), then shuffle that array. Define a function that maps a number to a coordinate, that would be `y = n / 8` and `x = n % 8` and there you are. – Jabberwocky Apr 16 '20 at 10:22
  • @Jabberwocky thank you for your suggestion. Suited my purpose just fine. If you could add it as an answer, I would be happy to accept – Ankit May 02 '20 at 17:35

2 Answers2

0

You could implement a pseudo random generator (PRG @ Wikipedia) with a period of 64 * 8. Use 3 bits for the axis with 8, and the remaining 6 bits for the axis with 64.

the busybee
  • 10,755
  • 3
  • 13
  • 30
0

Fill an array of length 512 with numbers increasing from from 0 to 511 (64x8 = 512), so the array will contain {0,1,2,3,..., 511}).

Then shuffle that array, for example like explained here: Shuffle array in C.

Then define a function that maps a number to a coordinate, that would be:

  • y = n / 8
  • x = n % 8

n being one of the numbers of the array.

If the array is well shuffled this guarantees that all pixels will be activatged in a random order.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115