[Solved] Patterns in a random QImage?



  • 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)!



  • 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();
    @



  • 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.



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

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.