Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Patterns in a random QImage?
Forum Updated to NodeBB v4.3 + New Features

[Solved] Patterns in a random QImage?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    owang.qt
    wrote on last edited by
    #1

    Hi, I have a basic QImage/QLabel question. I tried to fill an image with random values and display it on the screen. But for some reason, I can see very distinct repeated patterns. Is there something obvious that I am missing? Thanks!

    @
    srand(time(0));
    QImage random(w,h,QImage::Format_RGB888);
    random.fill(Qt::black);
    for(int j=0; j<h; j++) {
    for(int i=0; i<w; i++) {
    if((rand()%2) == 0) {
    random.setPixel(i, j, qRgb(255, 0, 0));
    }
    }
    }
    QLabel m_picture;
    m_picture->setPixmap(QPixmap::fromImage(random));
    m_picture->show();
    @

    and it comes out looking like this:

    !http://i.imgur.com/BrnY9.png(QImage Display)!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AcerExtensa
      wrote on last edited by
      #2

      maybe try this
      @
      QImage random(w,h,QImage::Format_RGB888);
      random.fill(Qt::black);
      for(int j=0; j<h; j++) {
      for(int i=0; i<w; i++) {
      srand(time(0));
      if((rand()%2) == 0) {
      random.setPixel(i, j, qRgb(255, 0, 0));
      }
      }
      }
      QLabel m_picture;
      m_picture->setPixmap(QPixmap::fromImage(random));
      m_picture->show();
      @

      God is Real unless explicitly declared as Integer.

      1 Reply Last reply
      0
      • O Offline
        O Offline
        owang.qt
        wrote on last edited by
        #3

        interestingly enough that does fix the problem, as does

        @
        float r = (float)rand()/(float)RAND_MAX;
        if(r<.5) {
        random.setPixel(i, j, qRgb(255, 0, 0));
        }
        @

        I guess this is one way to visualize the weakness of the c standard psuedorandom number generator in the ones digit.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          AcerExtensa
          wrote on last edited by
          #4

          rand() was always a week function in C...

          Please add "[SOLVED]" to topic subject if your problem is solved...

          God is Real unless explicitly declared as Integer.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved