lundi 24 septembre 2018

Why do I get this particular color pattern when using rand()?

I tried to create a bmp file, like this:

 uint8_t raw_r[pixel_width][pixel_height];
 uint8_t raw_g[pixel_width][pixel_height];
 uint8_t raw_b[pixel_width][pixel_height];
 uint8_t blue(uint32_t x, uint32_t y)
 {
     return (rand()%2)? (x+y)%rand() : ((x*y%1024)%rand())%2 ? (x-y)%rand() : rand();
 }
 uint8_t green(uint32_t x, uint32_t y)
 {
     return (rand()%2)? (x-y)%rand() : ((x*y%1024)%rand())%2 ? (x+y)%rand() : rand();
 }
 uint8_t red(uint32_t x, uint32_t y)
 {
     return (rand()%2)? (y-x)%rand() : ((x*y%1024)%rand())%2 ? (x+y)%rand() : rand();
 }

 for (y=0; y<pixel_height; ++y)
 {
     for (x=0; x<pixel_width; ++x)
     {
         raw_b[x][y]=blue(x, y);
         raw_g[x][y]=green(x, y);
         raw_r[x][y]=red(x, y);
     }
 }

I expected to get something random (white noise). However, the output is interesting:

enter image description here

Do you know the reason why?


Now, it is clear that it has nothing to do with rand(). Also try this code:

for (x=0; x<pixel_width; ++x)
         for (y=0; y<pixel_height; ++y)
         {
             r[x][y] = (x+y);
             g[x][y] = (y-x);
             /* b[x][y] = rand()%2? x : y; */
         }

enter image description here




Aucun commentaire:

Enregistrer un commentaire