Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Normalize random() output?



  • Team,

    Is it possible to normalize the output of random()?
    ie --- I only want to see an output of 8 digits?
    For now - I just see varying lengths on the output.

    Thanks!


  • Moderators

    @ShylerC
    It's not really a Qt question, but the answer isn't one either.

    To force a range on your random generator one usualy goes the following way

    outPut = random()%((max- min) + 1) + min; 
    

    for 8 digits min is 10000000 and max is 99999999, however due to the nature of this, it will "reduce" the randomness.



  • Thanks,

    I was looking at using QRandomGenerator too.
    I've gotten them both to output well. Just need to control their range to be purely 8 digits.
    Thank you so much!

    //to illustrate, test function -- QRandomGenerator Class//

    int yum;
    yum=700;

    while (yum < 800){
    QRandomGenerator::global();
    int x = QRandomGenerator::global()->generate64();
    yum = yum += 1;
    qDebug() << x;
    }

    /to illustrate, test function -- random()//
    int yum;
    yum=700;

    while (yum < 800){
    yum = yum += 1;
    x = random()
    qDebug() << x;
    }



  • @J.Hilk

    outPut = random()%((max- min) + 1) + min;

    I realize this is in a sense OT, but I've always been fascinated by (pseudo-) random number generation. You are using MOD (%) to restrict your range. My understanding is that this approach comes from The Devil if the range is "large" compared to random number return size.

    Random numbers have a random distribution of 0s and 1s in each bit. Suppose the random number generator returns an 8 bit number, 0 to 255. Suppose your range is 250, 0 to 249, so you use % 250. Most numbers will occur once, being equally likely. But for generated random numbers 250 to 255 inclusive, the MOD will return a number 0 to 5 inclusive. So 0 to 5 come out twice as likely to be returned compared to any other number!

    Any comments?


  • Moderators

    @JonB
    yep, that's about right, and the cause for my tagged comment about

    it will "reduce" the randomness.



  • @J.Hilk
    :) Is there a suggested algorithm somewhere for generating "truly" random numbers within a range, e.g. as per the case above, generalized for a range r? I can think of: if the random number is greater equal 250 throw it away and draw again, but obviously this does not scale well efficiency-wise....!


  • Moderators

    @JonB said in Normalize random() output?:

    @J.Hilk
    :) Is there a suggested algorithm somewhere for generating "truly" random numbers within a range, e.g. as per the case above, generalized for a range r? I can think of: if the random number is greater equal 250 throw it away and draw again, but obviously this does not scale well efficiency-wise....!

    Thats a tough question.

    if your target range is 0-3, 0-7, 0-15, 0-31, 0-63, 0-127, 69-127 etc , you could do some bit shifting/setting, but after that it gets complicated. I would suggest looking into boost libary or something like that those guys usually have some nifty solutions for such problems :)


  • Qt Champions 2017

    Scale and shift (for generation between 0 and generatorMax):

    int randomNumber, generatorMax;
    int max, min; //< Range
    int scaledNumber = min + static_cast<double>(randomNumber) / generatorMax * (max - min);
    

    PS. One should probably round at the end, instead of truncating, but I forgot x_x.


Log in to reply