[SOLVED] weird Qrand behaviour



  • hi,
    the use of recommanded qrand works nice when executed in PC or through the nokia simulator, but failed strangely to randomize when executed from the N8 itself.
    Clearly : A function return a randomized QList, whereas the same function return an ordered QList each time.

    I really don't see what is wrong here.

    @
    QList <int> Keys_Dialog::RandomKeys ()
    {
    QList <int> Keys_Set ;
    QList <int> RemainingList ;

    int MAX_KEYS_NB = 10                 ;           // define the max nr of keys to generate
    int RandomValue = 0,  nb_val         ;
    
    
    nb_val = Keys_Set.count()            ;
    for (int i=0; i < MAX_KEYS_NB; i ++)
    {
           RemainingList.append (i)      ;
    }
    
    while( RemainingList.count () > 0)
    {
      RandomValue = (int) (((long) qrand() * RemainingList.count())/ RAND_MAX)  ;
    
          Keys_Set.append (RemainingList.at(RandomValue))                           ;
          RemainingList.removeAt (RandomValue);
          nb_val = Keys_Set.count()       ;
    

    qDebug() << "Keys_Dialog::RandomKeys : " << Keys_Set ;
    }

    return Keys_Set                       ;
    

    }@



  • Hi,

    I've not tested, but I think this

    @RandomValue = (int) (((long) qrand() * RemainingList.count())/ RAND_MAX);@

    is always 0



  • Correct !

    This was due to a bad init of the random generator that is lower in the the mobile.

    thx a lot.


Log in to reply
 

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