简体   繁体   中英

Fill Grid With Random Pixels

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM